我的应用需要将照片上传到服务器。这在上传第一张拍摄的照片时效果很好。当我拍摄另一张照片时,上传失败。
这是我的代码:
// Upload files to server
function uploadFile(mediaFile) {
$.mobile.loading( 'show' );
var path = mediaFile;
alert(path);
var options = new FileUploadOptions();
options.mimeType="image/jpeg";
var params = new Object();
params.fullpath = path;
params.eventID = eventID;
options.params = params;
options.chunkedMode = true;
var ft = new FileTransfer();
ft.upload( path, "http://dev.tellthedj.de/db/uploadPhoto.php",
function(result) {
$.mobile.loading( 'hide' );
alert("Foto erfolgreich hochgeladen");
// Geschossenes Foto aus Cache löschen
var file = new FileEntry();
file.fullPath = mediaFile;
file.remove(success, fail);
function success(){
alert("success");
}
function fail(){
alert("cache löschen nicht erfolgreich")
}
},
function(error) {
$.mobile.loading( 'hide' );
alert("Upload nicht erfolgreich. Bitte checke deine Internet-Verbindung: ");
console.log(error.message);
},
options
);
}
mediaFile 是捕获的照片在缓存文件夹中的位置。我要上传的第二张照片总是调用“Upload nicht erfolgreich ...”,我在 LogCat 中遇到了这个错误
07-25 08:59:11.980: E/FileTransfer(6783): {"target":"http:\/\/dev.tellthedj.de\/db\/uploadPhoto.php","source":"file:\/\/\/storage\/sdcard0\/Android\/data\/com.phonegap.getting.started\/cache\/1374735548710.jpg","http_status":0,"code":3}
07-25 08:59:11.980: E/FileTransfer(6783): java.io.EOFException
07-25 08:59:11.980: E/FileTransfer(6783): at com.squareup.okhttp.internal.Util.readAsciiLine(Util.java:314)
07-25 08:59:11.980: E/FileTransfer(6783): at com.squareup.okhttp.internal.http.RawHeaders.fromBytes(RawHeaders.java:301)
07-25 08:59:11.980: E/FileTransfer(6783): at com.squareup.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:130)
07-25 08:59:11.980: E/FileTransfer(6783): at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:630)
07-25 08:59:11.980: E/FileTransfer(6783): at com.squareup.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:385)
07-25 08:59:11.980: E/FileTransfer(6783): at com.squareup.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:334)
07-25 08:59:11.980: E/FileTransfer(6783): at com.squareup.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:534)
07-25 08:59:11.980: E/FileTransfer(6783): at org.apache.cordova.core.FileTransfer$3.run(FileTransfer.java:443)
07-25 08:59:11.980: E/FileTransfer(6783): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-25 08:59:11.980: E/FileTransfer(6783): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-25 08:59:11.980: E/FileTransfer(6783): at java.lang.Thread.run(Thread.java:856)
07-25 08:59:11.980: E/FileTransfer(6783): Failed after uploading 25260 of 25260 bytes.
奇怪的是,第一次上传工作正常,第二次上传失败,错误代码为 3。第三次有效,第四次 -> 错误......