Phonegap Image Upload works only once

怎甘沉沦 提交于 2020-01-07 03:41:15

问题


my app needs to upload photos to a server. This works great when uploading the first captured photo. When I capture another photo, the upload fails.

Here is my code:

    // 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 is the location of the captured photo in cache folder. The second photo I want to upload always calls "Upload nicht erfolgreich..." and i got this error in 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.

The strange thing is, that the first upload works fine, the second fails with error code 3. The third works, the fourth -> error...


回答1:


Okay, I just spent the last day or so working through the same issue. What worked for me was to set the header to close the connection.

options.headers = { "Connection":"close" };

Found this answer on the cordova Bug Tracker site: https://issues.apache.org/jira/browse/CB-2293?focusedCommentId=13696238&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-13696238

Hope that helps, I know how frustrated I was with this one too!




回答2:


You can use xmlhttprequest level 2 via jquery. its easy and configure your java file with chrome environment. so your upload like web .




回答3:


This is an unfixed bug in the phonegap library, since there were no bug fixes, i had to get it work by my work around (Basically reupload on every alternate failure):

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);


来源:https://stackoverflow.com/questions/17851259/phonegap-image-upload-works-only-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!