2016-04-28 119 views
0

我想上傳圖像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

服務器無法從'file://'地址上傳,因爲它只能在本地PC上訪問。請嘗試使用在線圖片。 – Archer

+0

我認爲它發生的原因是因爲文件需要關聯到一個「圖像」參數。像一個阿賈克斯職位。 –

回答

0

問題在於需要將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); 
    } 
};