我正在开发Cordova 3.3.0 app + Angularjs。
我正在尝试进行多部分POST。 服务器正在等待一个文件,我无法上传base64字符串,我有存储在设备中的图像的真实路径。
如果我需要使用它,我还有base64图像。
cordova不支持输入文件。 显然,出于安全原因,我无法将值设置为输入文件。
所以我必须将或者base64转换为文件,然后进行发布。
//convert DOM img to file
var data ={
'lumen_moore_editar_usuario_rest[gender]':'M',
'lumen_moore_editar_usuario_rest[file]':file
};
$http({url: WS_EDIT_USER, method: 'POST',
data: $.param(data),
transformRequest: angular.identity,
headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
}).success(function(data, status, headers, config) {
console.log("EDITAR IMAGEN SUCCESS "+ data);
}).error(function(data, status, headers, config) {
console.log("EDITAR IMAGEN ERROR "+ data);
});
有可能吗? 我看了很多这个答案,但没有运气,我不想使用Cordova的FileTransfer.upload。
抱歉我的英文。
谢谢!
答案 0 :(得分:3)
我不得不使用Cordova FileTransfer,它第一次工作。 它包含在一项承诺的服务中。
editProfileUserImg:function(imageURI){
var deferred = $q.defer();
var options = new FileUploadOptions();
options.fileKey="file";
options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
var params = {};
options.params = params;
var headers={ 'Authorization':ACCESS_TOKEN.Authorization,
'X-WSSE':ACCESS_TOKEN.XWSSE};
options.headers = headers;
var ft = new FileTransfer();
ft.upload(imageURI, encodeURI(WS_CHANCE_PIC_USER),
function(r){
console.info("Code = " + r.responseCode);
console.info("Response = " + r.response);
console.info("Sent = " + r.bytesSent);
deferred.resolve(r.response);
},
function(error){
alert("An error has occurred: Code = " + error.code);
console.error("upload error source " + error.source);
console.error("upload error target " + error.target);
deferred.reject(error);
}, options);
return deferred.promise;
}