科尔多瓦图像上传与文件传输错误
问题描述:
我想上传图像imgur与科尔多瓦。 以下是我使用科尔多瓦图像上传与文件传输错误
var fileTransfer = {
startUpload: function(fileUrl) {
var uploadServer = 'https://api.imgur.com/3/image.json';
var apiKey = '<key id>';
/* global FileUploadOptions */
var options = new FileUploadOptions();
options.headers = {
'Authorization': apiKey
};
options.params = {};
ft = new FileTransfer();
ft.upload(fileUrl, encodeURI(uploadServer), fileTransfer.uploadSuccess, fileTransfer.uploadFail, options);
},
uploadSuccess: function(r) {
console.log(r);
},
uploadFail: function(error) {
console.log(error);
}
}
该错误消息的代码是:
{
body: "{data :{ error : No image data was sent to the upload api ,request :\/3\/image.json ,method :POST }, success :false, status :400}"
code: 1
exception: "https://api.imgur.com/3/image.json"
http_status: 400
source: "file:///storage/emulated/0/Android/data/com.hiapp.hiapp/cache/1461877896871.jpg"
target: "https://api.imgur.com/3/image.json"
}
但图像存在于源。
答
问题在于需要将filekey设置为“image”。 工作代码
var ft;
var fileTransfer = {
startUpload: function(fileUrl) {
var uploadServer = 'https://api.imgur.com/3/image/';
var options = new FileUploadOptions();
options.fileKey = 'image'; //This is the important point
options.fileName = fileUrl.substr(fileUrl.lastIndexOf('/') + 1);
options.mimeType = 'image/jpeg';
options.headers = {
'Authorization': 'Client-ID <the id>'
};
options.params = {};
ft = new FileTransfer();
ft.upload(fileUrl, encodeURI(uploadServer), fileTransfer.uploadSuccess, fileTransfer.uploadFail, options);
},
uploadSuccess: function(r) {
console.log(r);
},
uploadFail: function(err) {
console.log(err);
}
};
服务器无法从'file://'地址上传,因为它只能在本地PC*问。请尝试使用在线图片。 – Archer
我认为它发生的原因是因为文件需要关联到一个“图像”参数。像一个阿贾克斯职位。 –