【发布时间】:2017-09-13 14:08:24
【问题描述】:
在我的 phonegap 应用程序中,我用相机拍照,它按预期工作。
然后,我想将它发送到我的服务器。我发现发送 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);
}
}
在 try 分支的最后一行,我收到错误 FileTransferError.FILE_NOT_FOUND_ERR。
在此行之前的警报中,我在警报中显示路径(imagefilename 变量)。如果我尝试在我的 Android 设备中手动进入该路径,我将找不到它。路径是file:///storage/emulated/0/Android/data/com.My.App/cache/1505307795417.jpg
所以,我尝试设置选项
saveToPhotoAlbum: true
检查图像是否已保存以及我是否正确地在我的相册中看到了照片。我不知道为什么我在发送时收到错误,可能是路径错误?
我不认为问题出在服务器端,因为我什至在服务器日志中都看不到请求。
更新:
我也在Anuj T的建议下试过这个,但结果还是一样:
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);
【问题讨论】:
标签: android cordova phonegap-plugins file-transfer phonegap