我正在尝试使用cordova将上传图像上传到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"
}
但图像出现在源头。
答案 0 :(得分:0)
问题在于需要将filekey设置为&#34; image&#34;。 工作代码
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);
}
};