2013-02-09 41 views
1

我正在編寫例程以將圖像從我的iPhone(4S,iOS 6.1)上載到服務器。PhoneGap/Cordova FileTransfer.abort未按預期工作

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()停止它。

事實上,當我按下按鈕塞,我看到ft.abort()被調用,但傳遞繼續下去,直到補償

這裏日誌:

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 http://docs.phonegap.com/en/2.3.0/cordova_file_file.md.html#FileTransfer中聲明的錯誤。

正如日誌所述,映像已正確傳輸到服務器。

你對發生了什麼有什麼想法嗎?

回答

0

「if(progressEvent.lengthComputable){」 沒有匹配的右括號。

這可能是因爲onUploadSuccess和onUploadError後續方法被破壞。或者這也許只是你的問題中的一個錯字。

順便說一下,最好爲每個imageURI保存一個FileTransfer引用,而不是使用對單個FileTransfer的全局引用。然後,您可以一次上傳多張照片,並仍保留單獨取消每次上傳的功能,例如,

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

...然後在uploadPhoto創建一個新的文件傳輸後:

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

...然後通過imageURI在取消按鈕:

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

...記住在下載完成時或出現錯誤時從地圖上移除英尺:

fileTransferMap.removeItem(imageURI);