Phonegap图片上传仅适用一次

时间:2013-07-25 07:08:29

标签: ajax file-upload cordova file-transfer photo-upload

我的应用需要将照片上传到服务器。这在上传第一张拍摄的照片时效果很好。当我拍摄另一张照片时,上传失败。

这是我的代码:

    // 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是缓存文件夹中捕获的照片的位置。 我要上传的第二张照片总是调用“上传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.第三个工作,第四个 - >错误...

3 个答案:

答案 0 :(得分:2)

好的,我只是在最后一天工作了同样的问题。对我有用的是设置标题以关闭连接。

options.headers = {“Connection”:“close”};

在cordova Bug Tracker网站上找到了这个答案: https://issues.apache.org/jira/browse/CB-2293?focusedCommentId=13696238&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-13696238

希望有所帮助,我知道我对这个有多么沮丧!

答案 1 :(得分:0)

您可以通过jquery使用xmlhttprequest level 2。 它很容易并使用chrome环境配置您的java文件。 所以你的上传就像网络一样。

答案 2 :(得分:0)

这是phonegap库中一个未修复的错误,因为没有错误修复,我不得不通过我的工作来解决它(基本上每次备用失败都会重新上载):

ft.upload(path,
          encodeURI("http://yourdomain.com/upload.php"),
            function(result) {
                alert("Uploaded");
            },
            function(error) {
                ft.upload(path,
                encodeURI("http://yourdomain.com/upload.php"),
                function(result) {
                    alert("Uploaded");                  
                },
                function(error) {
                    alert("Error uploading image");
                },
                { fileName: name, fileKey: "file", mimeType: "image/jpeg", chunkedMode: false }, true);     
            },
            { fileName: name, fileKey: "file", mimeType: "image/jpeg", chunkedMode: false }, true);