本文介绍了PhoneGap / Cordova FileTransfer.abort无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个例程,将图像从iPhone(4S,iOS 6.1)上传到服务器。

I am writing a routine to upload images from my iPhone (4S, iOS 6.1) to a server.

var ft = undefined;

var e = document.getElementById('stopper');
e.addEventListener('click', onStopUploadBtn, false);

function uploadPhoto(imageURI) {

    console.log('Preparing to upload' + imageURI);

    ...

    ft = new FileTransfer();
    ft.onprogress = onProgress;
    ft.upload(imageURI, url, onUploadSuccess, onUploadError, uploadOptions);
    console.log('upload started');
}

function onStopUploadBtn() {
    if(ft) {
        console.log('Aborting');
        ft.abort(onUploadSuccess, onUploadError);
    }
}

function onProgress(progressEvent) {
    if (progressEvent.lengthComputable) {
        console.log(progressEvent.loaded / progressEvent.total);
}

function onUploadSuccess(response) {
    console.log("Code = " + response.responseCode);
    console.log("Response = " + response.response);
    console.log("Sent = " + response.bytesSent);
}

function onUploadError(error) {
    alert("An error has occurred: Code = " = error.code);
}

上传的过程很吸引人,但我无法停止.abort()。

The upload works like a charm, but I am not able to stop it with .abort().

实际上,当我按下停止按钮时,我看到ft.abort()被调用了,但是传输一直进行到编译完成

In fact, when a I press the stopper button, I see that ft.abort() is invoked, but the transfer keep going, till comp

以下是日志:

2013-02-09 12:29:18.422 HelloWorld[855:907] Multi-tasking -> Device: YES, App: YES

2013-02-09 12:29:22.496 HelloWorld[855:907] [LOG] opening camera

2013-02-09 12:29:30.728 HelloWorld[855:907] [LOG] Preparing to uploadfile://localhost/var/mobile/Applications/C82E5A93-D276-4380-8420-39165C9644C4/tmp/cdv_photo_014.jpg

2013-02-09 12:29:30.729 HelloWorld[855:907] [LOG] upload ready to go

2013-02-09 12:29:30.732 HelloWorld[855:907] -[CDVFileTransfer requestForUploadCommand:fileData:][Line 207] fileData length: 1016478
2013-02-09 12:29:30.734 HelloWorld[855:907] [LOG] upload started
2013-02-09 12:29:31.208 HelloWorld[855:907] [LOG] 0.03223007996537784
2013-02-09 12:29:31.210 HelloWorld[855:907] [LOG] 0.06446015993075568
2013-02-09 12:29:31.213 HelloWorld[855:907] [LOG] 0.09669023989613353

...

2013-02-09 12:29:32.057 HelloWorld[855:907] [LOG] 0.16511030894372916
2013-02-09 12:29:32.113 HelloWorld[855:907] [LOG] 0.167868278432954
2013-02-09 12:29:32.172 HelloWorld[855:907] [LOG] 0.17062624792217884

2013-02-09 12:29:32.188 HelloWorld[855:907] [LOG] Aborting
2013-02-09 12:29:32.191 HelloWorld[855:907] FileTransferError {
    code = 4;
    source = "file://localhost/var/mobile/Applications/C82E5A93-D276-4380-8420-39165C9644C4/tmp/cdv_photo_014.jpg";
    target = "...";
}

2013-02-09 12:29:32.229 HelloWorld[855:907] [LOG] 0.17338421741140367

...

2013-02-09 12:29:45.641 HelloWorld[855:907] [LOG] 0.9939470241666585
2013-02-09 12:29:45.698 HelloWorld[855:907] [LOG] 0.9967049936558833
2013-02-09 12:29:45.757 HelloWorld[855:907] [LOG] 0.9991324789267132
2013-02-09 12:29:45.813 HelloWorld[855:907] [LOG] 1
2013-02-09 12:30:17.200 HelloWorld[855:907] File Transfer Finished with response code 200
2013-02-09 12:30:17.203 HelloWorld[855:907] [LOG] Code = 200
2013-02-09 12:30:17.204 HelloWorld[855:907] [LOG] Response = 17
2013-02-09 12:30:17.205 HelloWorld[855:907] [LOG] Sent = 1016692

.abort()被正确调用,因为我收到API 。

The .abort() is called properly, as I receive the error stated in the API's http://docs.phonegap.com/en/2.3.0/cordova_file_file.md.html#FileTransfer .

即时消息

您是否知道发生了什么?

Do you have any idea on what is going on?

推荐答案

如果(progressEvent.lengthComputable){
没有匹配的结束括号。

"if (progressEvent.lengthComputable) {"has no matching closing bracket.

是那会破坏后续方法onUploadSuccess和onUploadError。

It could be that's broken subsequent methods onUploadSuccess and onUploadError. Or perhaps that's just a typo in your question.

顺便说一句,最好为每个imageURI保存一个FileTransfer引用,而不是对单个图像使用全局引用。文件传输。然后,您可以一次上传多张照片,并且仍然保留分别取消每次上传的功能,例如

Incidentally, it might be good practice to save a FileTransfer reference for each imageURI, rather than using a global reference to a single FileTransfer. Then you could upload multiple photos at once, and still retain the ability to cancel each upload separately, e.g.

// basic implementation of hash map of FileTransfer objects
// so that given a key, an abort function can find the right FileTransfer to abort
function SimpleHashMap()
{
    this.items = {};
    this.setItem = function(key, value) { this.items[key] = value; }
    this.getItem = function(key)
                   {
                       if (this.hasItem(key)) { return this.items[key]; }
                       return undefined;
                   }
    this.hasItem = function(key) { return this.items.hasOwnProperty(key); }
    this.removeItem = function(key)
                      {
                          if (this.hasItem(key)) { delete this.items[key]; }
                      }
}
var fileTransferMap = new SimpleHashMap();

...然后在创建新的FileTransfer之后在uploadPhoto中:

...then in uploadPhoto after creating a new FileTransfer:

// register this object so that abort can find it
fileTransferMap.setItem(imageURI, ft);

...然后在取消按钮中传递imageURI:

...then pass the imageURI in your cancel button:

function onStopUploadBtn(imageURI)
{
    var ft = fileTransferMap.getItem(imageURI);
    if (ft)
    {
        console.log('Aborting');
        ft.abort(onUploadSuccess, onUploadError);
    }
}

...并记得从下载完成或出现错误时进行映射:

...and remember to remove the ft from the map when download is complete, or upon error:

fileTransferMap.removeItem(imageURI);

这篇关于PhoneGap / Cordova FileTransfer.abort无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-25 13:57