cordovaでcordova-plugin-file-transferを使って、
写真をサーバーへアップロードるするAndroidアプリを作成しています。
わたくしの後述のコードでは、下記のconsole logが出力されます。
Successful file write...
fileURL : file:///data/data/com.adobe.phonegap.app/cache/.Pic.jpg%3F1530809009472
Successful upload...
fileEntry.fullPath = /.Pic.jpg?1530809009472
Code = 200
response =
bytesSent = 0
このログより、Androidからサーバーへのアクセスは成功しているようですが、
ファイルの転送が0バイト、つまり転送されていないようです。
また、サーバー側では、ファイルは保存されていますが
サイズが0バイトとなっていました。
どなたか、解決策が分かる方、ご回答よろしくお願い致します。
また、他に必要な情報がありましたら、ご指摘お願い致します。
バージョン:
Phonegap Buiid : 7.1.0
Android : 5.0.1
cordova-plugin-file : 6.0.1
cordova-plugin-file-transfer : 1.7.1
ソースコード:
//select and upload picture
$(document).on("click", ".pic-button", function() {
openFilePicker();
});
function openFilePicker(selection) {
var srcType = Camera.PictureSourceType.SAVEDPHOTOALBUM;
var options = setOptions(srcType);
if (selection == "picker-thmb") {
// To downscale a selected image,
// Camera.EncodingType (e.g., JPEG) must match the selected image type.
options.targetHeight = 100;
options.targetWidth = 100;
}
navigator.camera.getPicture(function cameraSuccess(imageUri) {
// Do something with image
console.log("get picture");
console.log("imageUri : "+imageUri);
image_uri = imageUri;
URItoFileEntry(imageUri);
onUploadFile(imageUri);
}, function cameraError(error) {
//console.debug("Unable to obtain picture: " + error, "app");
console.log("Unable to obtain picture: " + error, "app");
}, options);
}
function setOptions(srcType) {
var options = {
// Some common settings are 20, 50, and 100
quality: 50,
destinationType: Camera.DestinationType.FILE_URI,
// In this app, dynamically set the picture source, Camera or photo gallery
sourceType: srcType,
encodingType: Camera.EncodingType.JPEG,
mediaType: Camera.MediaType.PICTURE,
allowEdit: true,
correctOrientation: true //Corrects Android orientation quirks
}
return options;
}
function URItoFileEntry(imageURI) {
console.log("URItoFileEntry");
window.resolveLocalFileSystemURL(imageURI, fileEntryToArrayBuffer, onError);
}
function fileEntryToArrayBuffer(fileEntry) {
console.log("fileEntryToArrayBuffer");
console.log(fileEntry.name);
file_entry_name = image_uri.substr(image_uri.lastIndexOf('/') + 1);
fileEntry.file( function(file) {
console.log("fileEntry.file");
var reader = new FileReader();
reader.onloadend = ArrayBufferToBlob;
reader.readAsArrayBuffer(file);
}, onError2);
}
function ArrayBufferToBlob(event) {
console.log("ArrayBufferToBlob");
blob = new Blob([event.target.result], { type: "image/jpeg" });
console.log("blob.size : "+blob.size);
console.log("blob.type : "+blob.type);
}
function onError() {
console.log("onError");
}
function onError2() {
console.log("onError2");
}
function onUploadFile(imageUri) {
console.log("onUploadFile");
console.log("imageUri : "+imageUri);
window.requestFileSystem(window.TEMPORARY, 5 * 1024 * 1024, function (fs) {
console.log('file system open: ' + fs.name);
var dirEntry = fs.root;
console.log("imageUri : "+imageUri);
dirEntry.getFile(imageUri, { create: true, exclusive: false }, function (fileEntry) {
console.log("fileEntry name");
console.log(fileEntry.name);
// Write something to the file before uploading it.
console.log("writeFile");
writeFile(fileEntry);
}, onErrorCreateFile);
}, onErrorLoadFs);
console.log("onUploadFile end");
}
function writeFile(fileEntry, dataObj) {
// Create a FileWriter object for our FileEntry (log.txt).
fileEntry.createWriter(function (fileWriter) {
fileWriter.onwriteend = function () {
console.log("Successful file write...");
upload(fileEntry);
};
fileWriter.onerror = function (e) {
console.log("Failed file write: " + e.toString());
console.log(e.toString());
};
if (!dataObj) {
if(blob == undefined) {
console.log("blob undefined");
} else {
console.log("blob defined");
dataObj = blob;
}
}
fileWriter.write(dataObj);
}, failCreateWriter);
}
function failCreateWriter(error) {
console.log(error.code);
}
function upload(fileEntry) {
// !! Assumes variable fileURL contains a valid URL to a text file on the device,
var fileURL = fileEntry.toURL();
var success = function (r) {
console.log("Successful upload...");
console.log("fileEntry.fullPath = " + fileEntry.fullPath);
console.log("Code = " + r.responseCode);
console.log("response = " + r.response);
console.log("bytesSent = " + r.bytesSent);
}
var fail = function (error) {
ons.notification.alert("An error has occurred: Code = " + error.code);
}
function displayFileData(data){
console.log("displayFileData");
//ons.notification.alert(data);
}
var options = new FileUploadOptions();
options.chunkedMode = false;
options.fileKey = "file";
options.fileName = file_entry_name;
options.mimeType = "image/jpeg";
var params = {};
params.value1 = "test";
params.value2 = "param";
options.params = params;
console.log("fileURL : "+fileURL);
var ft = new FileTransfer();
// SERVER must be a URL that can handle the request, like
// http://some.server.com/upload.php
var url_post_image = "http://some.server.com/upload.php";
ft.upload(fileURL, encodeURI(url_post_image), success, fail, options);
};
function onErrorLoadFs() {
console.log("onErrorLoadFs");
}
function onErrorCreateFile() {
console.log("onErrorCreateFile");
}