在我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.fullPath?它必須設置爲options.fileName的值? –
看我的更新。由於ft.upload在resolveLocalFileSystemURL之前執行,所以我更改了一些代碼。結果仍然沒有找到文件...你知道爲什麼嗎? –
可以嘗試靜態fileurl路徑.. ??因爲我不能在我身邊重新創建問題..只是嘗試「var fileURL =」///storage/emulated/0/DCIM/yourpic.jpg「 看看是否有效......確保yourpic.jpg存在你的文件系統 –