0

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

这是我的代码:

    // 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。第三次有效,第四次 -> 错误......

4

3 回答 3

2

好的,我刚刚花了最后一天左右的时间来解决同样的问题。对我有用的是设置标题以关闭连接。

options.headers = { "连接":"关闭" };

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

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

于 2013-08-23T22:21:06.947 回答
0

您可以通过 jquery 使用 xmlhttprequest 级别 2。它很容易并使用 chrome 环境配置您的 java 文件。所以你的上传就像 web 。

于 2013-07-25T08:00:47.770 回答
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);
于 2013-09-07T05:07:10.403 回答