科尔多瓦/ PhoneGap的:FileTransferError.FILE_NOT_FOUND_ERR与文件传输的插件
问题描述:
在我PhoneGap的应用程序,我带着我的相机拍摄照片,并将其按预期工作。科尔多瓦/ PhoneGap的:FileTransferError.FILE_NOT_FOUND_ERR与文件传输的插件
然后,我想将它发送到我的服务器。我看到,发送base64编码字符串是一个不好的做法,我想最好的解决办法是使用文件传输插件。
所以,我加入了插件,我写了这个:
function onPhotoURISuccess(imageURI) {
try{
var url = "myserver/addPhoto";
alert(url);
var options = new FileUploadOptions();
options.chunkedMode = false;
options.fileKey = "recFile";
var imagefilename = imageURI;
options.fileName = imagefilename;
options.mimeType = "image/jpeg";
options.params = { "token": APP.TOKEN};
var ft = new FileTransfer();
alert(imagefilename);
ft.upload(imageURI, url, win, fail, options);
}
catch (err) {
alert(err.message);
}
}
在尝试分支中的最后一行,我得到的错误FileTransferError.FILE_NOT_FOUND_ERR。
在此行之前的警报中,我在警报中显示路径(imagefilename变量)。如果我尝试手动去在我的Android设备这条道路,我找不到它。路径是文件:///storage/emulated/0/Android/data/com.My.App/cache/1505307795417.jpg
所以,我试图设置的选项
saveToPhotoAlbum: true
检查图像是否保存,我正确地看到我的相册中的照片。我不知道为什么我发送它时发生错误,可能是路径错误?
我不认为这个问题是服务器端,因为我甚至无法看到服务器日志的请求。
UPDATE:
我也试过之后Anuj牛逼的建议,但结果还是一样:
function onPhotoURISuccess(imageURI) {
var filepath;
window.resolveLocalFileSystemURL(imageURI, function success(fileEntry) {
// Do something with the FileEntry object, like write to it, upload it, etc.
// writeFile(fileEntry, imageURI);
filepath = fileEntry.fullPath;
alert("got file: " + fileEntry.fullPath);
// displayFileData(fileEntry.nativeURL, "Native URL");
try {
var url = "myUrl";
alert(url);
var options = new FileUploadOptions();
options.chunkedMode = false;
options.fileKey = "recFile";
var imagefilename = filepath;
options.fileName = imagefilename;
options.mimeType = "image/jpeg";
options.params = { "token": APP.TOKEN }; // if we need to send parameters to the server request
var ft = new FileTransfer();
alert(imagefilename);
alert(imageURI);
ft.upload(filepath, url, win, fail, options);
}
catch (err) {
alert(err.message);
}
}, function() {
// If don't get the FileEntry (which may happen when testing
// on some emulators), copy to a new FileEntry.
alert("file system fail");
createNewFileEntry(imgUri);
});
}
更新2:
这是我的服务器端码。
internal GenericResponse AddChiusuraPhoto(string token)
{
Utility.Logger("AddChiusuraPhoto");
var gr = new GenericResponse();
if (CheckToken(token, out IS_UTENZE utente))
{
try
{
var md5 = new md5Manager();
HttpPostedFile file = HttpContext.Current.Request.Files["recFile"];
if (file == null)
return null;
string targetFilePath = @"C:\ProgramData\" + file.FileName;
file.SaveAs(targetFilePath);
Utility.Logger("[AddChiusuraPhoto] Returning lista ");
return gr;
}
catch (Exception ex)
{
gr.ESITO = "[KO]";
gr.MESSAGGIO = ex.ToSafeString();
Utility.Logger("AddChiusuraPhoto " + gr.MESSAGGIO);
}
}
else
{
gr.ESITO = "[KO]";
gr.MESSAGGIO = "Utente non loggato, impossibile effettuare il logout.";
Utility.Logger("AddChiusuraPhoto " + gr.MESSAGGIO);
}
return gr;
}
请注意,这是一个WCF。这是解决部分:
[OperationContract]
[WebInvoke(UriTemplate = "chiusure/addPhoto", Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
GenericResponse AddChiusuraPhoto(string token);
答
你需要如果你需要使用一个FileEntry的物体进行拍照,并得到一个FileEntry的对象
中,destinationType设置为Camera.DestinationType.FILE_URI在CameraOptions对象
更新的代码
function getPicture(){
var srcType = Camera.PictureSourceType.CAMERA;
var options = setOptions(srcType);
//var func = createNewFileEntry;
navigator.camera.getPicture(function cameraSuccess(imageUri) {
//displayImage(imageUri);
// You may choose to copy the picture, save it somewhere, or upload.
//func(imageUri);
var filename = imageUri.substr(imageUri.lastIndexOf('/') + 1);
window.resolveLocalFileSystemURL(cordova.file.cacheDirectory, function success(dirEntry) {
// Do something with the FileEntry object, like write to it, upload it, etc.
// writeFile(fileEntry, imgUri);
console.log("got file: " + dirEntry.fullPath);
console.log("file name " + filename);
// JPEG file
dirEntry.getFile(filename, { create: true, exclusive: false }, function (fileEntry) {
// Do something with it, like write to it, upload it, etc.
// writeFile(fileEntry, imgUri);
console.log("got file: " + fileEntry.fullPath);
// displayFileData(fileEntry.fullPath, "File copied to");
upload(fileEntry);
}, onErrorCreateFile);
// displayFileData(fileEntry.nativeURL, "Native URL");
}, function() {
// If don't get the FileEntry (which may happen when testing
// on some emulators), copy to a new FileEntry.
//createNewFileEntry(imgUri);
});
//console.log(imageUri);
}, function cameraError(error) {
console.debug("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.NATIVE_URI ,
// In this app, dynamically set the picture source, Camera or photo gallery
sourceType: srcType,
encodingType: Camera.EncodingType.JPEG,
mediaType: Camera.MediaType.PICTURE,
allowEdit: false,
saveToPhotoAlbum:false,
correctOrientation: true //Corrects Android orientation quirks
};
return options;
}
查看详细说明here。
要将文件上传到服务器
function upload(fileEntry) {
var fileURL = fileEntry.toURL();
var success = function (r) {
console.log("Successful upload..." + JSON.stringify(r));
console.log("Code = " + JSON.stringify(r.responseCode));
alert(JSON.stringify(r.response));
// displayFileData(fileEntry.fullPath + " (content uploaded to server)");
};
var fail = function (error) {
alert("An error has occurred: Code = " + error.code);
console.log("Error= " + error);
};
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 SERVER = "http://posttestserver.com/post.php";
var ft = new FileTransfer();
// SERVER must be a URL that can handle the request, like
// http://some.server.com/upload.php
ft.upload(fileURL, encodeURI(SERVER), success, fail, options);
}
,当你得到你也可以检查您的PARAMS上传到服务器或没有成功警报,您将收到成功的链接。
希望这有助于。
感谢您的回复。那么,我该如何使用fileEntry.fullPath?它必须设置为options.fileName的值? –
看我的更新。由于ft.upload在resolveLocalFileSystemURL之前执行,所以我更改了一些代码。结果仍然没有找到文件...你知道为什么吗? –
可以尝试静态fileurl路径.. ??因为我不能在我身边重新创建问题..只是尝试“var fileURL =”///storage/emulated/0/DCIM/yourpic.jpg“ 看看是否有效......确保yourpic.jpg存在你的文件系统 –