
連携サービス
設定ファイル
API一覧
© 2025 KT Partners CO.,LTD.
ローカルのファイルシステム上のファイルをサーバーにHTTPでアップロードする機能、および、サーバー上のファイルをHTTPでダウンロードして、ローカルのファイルシステムにファイルとして保存する機能です。
アプリカンAPIのver.1.0系のFileSystemのFileTransferのリファレンスです
| onprogress: Function | データチャンクが転送されるたびに呼び出されます。この関数には、引数にProgressEventオブジェクトが渡されます。 |
|---|
upload(fileURL, encodeURI, Function successCallback, Function errorCallback, options, trustAllHosts)
ファイルをアップロードします。アップロード先に同名のファイルが存在する場合は移動先のファイルを削除します。
| fileURL | アップロードファイルのパスをフルパスで指定します。 |
|---|---|
| encodeURI | ファイルを受け取るサーバーのURLを指定します。 |
| successCallback : Function | アップロード成功時にMetadataオブジェクトがCallback関数に引数として渡されます。 |
| errorCallback : Function | アップロード失敗時にエラーコードを含んだイベントオブジェクトがCallback関数に引数として渡されます。 |
| options | オプションのパラメータは以下の通りです。
|
| trustAllHosts : Boolean | 自己署名されたセキュリティ証明書を許可するかどうかの設定です。デフォルトは [false] に設定されています。 Androidは自己署名されたセキュリティ証明書を拒否するので、その際に役立ちますが、リリース時には推奨されません。この機能はAndroid・iOSでサポートされています。 |
var win = function (r) {
console.log("Code = " + r.responseCode);
console.log("Response = " + r.response);
console.log("Sent = " + r.bytesSent);
}
var fail = function (error) {
alert("An error has occurred: Code = " + error.code);
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
}
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = fileURL.substr(fileURL.lastIndexOf('/') + 1);
options.mimeType = "text/plain";
var params = {};
params.value1 = "test";
params.value2 = "param";
options.params = params;
var ft = new FileTransfer();
ft.upload(fileURL, encodeURI("http://some.server.com/upload.php"), win, fail, options);
アップロード・ヘッダーおよび進行イベントを備えた例
function win(r) {
console.log("Code = " + r.responseCode);
console.log("Response = " + r.response);
console.log("Sent = " + r.bytesSent);
}
function fail(error) {
alert("An error has occurred: Code = " + error.code);
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
}
var uri = encodeURI("http://some.server.com/upload.php");
var options = new FileUploadOptions();
options.fileKey="file";
options.fileName=fileURL.substr(fileURL.lastIndexOf('/')+1);
options.mimeType="text/plain";
var headers={'headerParam':'headerValue'};
options.headers = headers;
var ft = new FileTransfer();
ft.onprogress = function(progressEvent) {
if (progressEvent.lengthComputable) {
loadingStatus.setPercentage(progressEvent.loaded / progressEvent.total);
} else {
loadingStatus.increment();
}
};
ft.upload(fileURL, uri, win, fail, options);
ファイルアップード成功後にCallback関数に引数として渡されるオブジェクトです。
| bytesSent | アップロード時にサーバに送信されたバイト数です。 |
|---|---|
| responseCode | アップロード時にサーバより渡されたレスポンスコードです。 |
| response : String | アップロード時にサーバより渡されたレスポンスです。 |
| headers : Object | アップロード時にサーバより渡されたレスポンスヘッダです。 ※iOSのみこの機能をサポートしています。 |
※iOS QuirksはresponseCodeまたはbytesSentをサポートしていません。
download(source, target, Function successCallback, Function errorCallback, trustAllHosts, options)
ファイルをダウンロードします。ダウンロード先に同名のファイルが存在する場合は移動先のファイルを削除します。
| source | ファイルをダウンロードするサーバのURLを指定します。(encodeURI()によってコード化されます。) |
|---|---|
| target | ダウンロードするファイルURLを指定します。 |
| successCallback : Function | ダウンロード成功時にFileEntryオブジェクトがCallback関数に引数として渡されます。 |
| errorCallback : Function | ダウンロード失敗時にエラーコードを含んだイベントオブジェクトがCallback関数に引数として渡されます。 |
| trustAllHosts : Boolean | 自己署名されたセキュリティ証明書を許可するかどうかの設定です。デフォルトは [false] に設定されています。 Androidは自己署名されたセキュリティ証明書を拒否するので、その際に役立ちますが、リリース時には推奨されません。この機能はAndroid・iOSでサポートされています。 |
| options | オプション用のパラメータですが、現在サポートされているのは、リソースへのアクセス件を指定する機能のみです。 |
var button_el = null;
var progress_el = null;
var pre_el = null;
var video_el = null;
document.addEventListener("deviceready", function() {
button_el = document.querySelector("button");
progress_el = document.querySelector("progress");
pre_el = document.querySelector("pre");
video_el = document.querySelector("video");
// button要素にclickイベントのリスナーをセット
button_el.addEventListener("click", start, false);
}, false);
// 処理開始(ダウンロードファイルを保存する空のファイルを生成)
function start() {
button_el.disabled = true;
pre_el.textContent = "ダウンロード準備中...";
// ファイルシステムにアクセス
applican.requestFileSystem(
LocalFileSystem.PERSISTENT, // 永続的なファイルシステム
10*1024*1024, // 10MB
function(fs) { // ファイルシステムへのアクセス成功時のコールバック関数
// ファイルシステムのルート直下にファイルを生成
fs.root.getFile(
"test.mp4",
{ create: true, exclusive: false },
function(entry) { // ファイル生成成功時のコールバック関数
// File URL を取得してダウンロード開始
downloadStart(entry.toURL());
},
function(error) { // ファイル生成失敗時のコールバック関数
pre_el.textContent = "ファイルの生成に失敗しました (" + error.code + ")";
}
);
},
function(error) { // ファイルシステムへのアクセス失敗時のコールバック関数
pre_el.textContent = "ファイルシステムへのアクセスに失敗しました (" + error.code + ")";
}
);
}
// ダウンロード開始
function downloadStart(file_url) {
var download_url = "http://rd.newphoria.com/npp/targets/horizontal/scenes/s06.mp4";
// FileTransferオブジェクトを生成
var ft = new FileTransfer();
// ダウンロードの進捗表示関数を定義
ft.onprogress = showDownloadProgress;
// ダウンロード開始
ft.download(
encodeURI(download_url), // ダウンロードURL
file_url, // ファイルURL
downloadSuccessCb, // 成功時のコールバック関数
downloadErrorCb, // 失敗時のコールバック関数
false,
null
);
}
// ダウンロードの進捗表示
function showDownloadProgress(event) {
// progress要素をアップデート
if(event.lengthComputable) {
var percent = parseInt(event.loaded * 100 / event.total, 10);
pre_el.textContent = "ダウンロード中... (" + percent + " %)";
progress_el.value = percent;
} else {
progress_el.removeAttribute("value");
}
}
// ダウンロード成功時の処理
function downloadSuccessCb(entry) {
video_el.src = entry.toURL();
video_el.play();
pre_el.textContent = "ダウンロードが完了しました。 " + video_el.src;
}
// ダウンロード失敗時の処理
function downloadErrorCb(error) {
var msg = "";
switch(error.code) {
case FileTransferError.FILE_NOT_FOUND_ERR:
msg = "指定の File URL に関連付けられたファイルエントリが見つかりませんでした。"
case FileTransferError.INVALID_URL_ERR:
msg = "指定の File URL が不適切です。";
case FileTransferError.CONNECTION_ERR:
msg = "指定の File URL に関連付けられたファイルへのアクセスに失敗しました。";
case FileTransferError.ABORT_ERR:
msg = "中止要求のため処理を中止しました。";
case FileTransferError.NOT_MODIFIED_ERR:
msg = "指定の File URL に関連付けれたファイルは読み取り専用のため書き込むことができませんでした。";
default:
msg = "未知のエラーが発生しました。";
}
pre_el.textContent = msg;
}
abort()
transferメソッドを中止する機能です。コールバックにはFileTransferErrorオブジェクトが渡されます。
var win = function(r) {
console.log("Should not be called.");
}
var fail = function(error) {
// error.code == FileTransferError.ABORT_ERR
alert("An error has occurred: Code = " + error.code);
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
}
var options = new FileUploadOptions();
options.fileKey="file";
options.fileName="myphoto.jpg";
options.mimeType="image/jpeg";
var ft = new FileTransfer();
ft.upload(fileURL, encodeURI("http://some.server.com/upload.php"), win, fail, options);
ft.abort();