2016-09-26 56 views
3

我想通過在AngularJs中的$http提供者發佈FormData對象。使用Angular/Cordova上傳文件

在瀏覽器中,可以將文件值input[type=file]附加到FormData並通過$http發佈。例如:

formData.append('file', inp.files[0], 'nai.jpg'); 
formData.append('text', 'hello'); 
$http.post('url', formData, { 
    transformRequest: angular.identity, 
    headers: { 'Content-Type': undefined } 
}); 

並且服務器獲得了帶有兩個主體字段的發佈請求。但在科爾多瓦的應用程序,input[type=file]不可用,這樣我就可以從用戶通過本地插件像這樣得到fileURI

navigator.camera.getPicture(function(fileURI) { 
    // i got fileURI 
}); 

現在,請給我解釋一下我怎樣才能把fileURI作爲文件formData和發送由$http提供商的Angular(不是FileTransfer)。

回答

0

如果我們在配置對象中指定選項,Cordova還會提供圖像的base64數據。以便您可以將相同的base64數據傳遞給該服務。

var cameraOptions = { 
    destinationType : 0 /*(DATA_URL -- For base64 data)*/ 
} 
navigator.camera.getPicture(cameraSuccess, cameraError, cameraOptions); 
+0

thx。是的,但這種方式太重了。想象一下,在我提交之前,我應該將5mb的字符串保存到JavaScript變量中。 – Nainemom