Wednesday, 15 April 2015

iphone - PhoneGap/Cordova FileTransfer.abort not working as expected -



iphone - PhoneGap/Cordova FileTransfer.abort not working as expected -

i writing routine upload images iphone (4s, ios 6.1) server.

var ft = undefined; var e = document.getelementbyid('stopper'); e.addeventlistener('click', onstopuploadbtn, false); function uploadphoto(imageuri) { console.log('preparing 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); }

the upload works charm, not able stop .abort().

in fact, when press stopper button, see ft.abort() invoked, transfer maintain going, till comp

here log:

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 photographic camera 2013-02-09 12:29:30.728 helloworld[855:907] [log] preparing 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 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 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

the .abort() called properly, receive error stated in api's http://docs.phonegap.com/en/2.3.0/cordova_file_file.md.html#filetransfer .

the image correctly transferred server, log says.

do have thought on going on?

"if (progressevent.lengthcomputable) {" has no matching closing bracket.

it that's broken subsequent methods onuploadsuccess , onuploaderror. or perhaps that's typo in question.

incidentally, might practice save filetransfer reference each imageuri, rather using global reference single filetransfer. upload multiple photos @ once, , still retain ability cancel each upload separately, e.g.

// basic implementation of hash map of filetransfer objects // given key, abort function can find right filetransfer abort function simplehashmap() { this.items = {}; this.setitem = function(key, value) { this.items[key] = value; } this.getitem = function(key) { if (this.hasitem(key)) { homecoming this.items[key]; } homecoming undefined; } this.hasitem = function(key) { homecoming this.items.hasownproperty(key); } this.removeitem = function(key) { if (this.hasitem(key)) { delete this.items[key]; } } } var filetransfermap = new simplehashmap();

...then in uploadphoto after creating new filetransfer:

// register object abort can find filetransfermap.setitem(imageuri, ft);

...then pass imageuri in cancel button:

function onstopuploadbtn(imageuri) { var ft = filetransfermap.getitem(imageuri); if (ft) { console.log('aborting'); ft.abort(onuploadsuccess, onuploaderror); } }

...and remember remove ft map when download complete, or upon error:

filetransfermap.removeitem(imageuri);

iphone cordova upload

No comments:

Post a Comment