2013-10-31 24 views
0

我已經在一個新類型的調查問題的前端工作,該問題允許用戶上傳圖像文件。鑑於jQuery本身不支持文件上傳,我試圖使用jQuery File Upload plugin使用Blueimp將文件添加到JSON jQuery文件上傳插件

我無法編寫自定義方法來通過插件獲取文件並將其插入到我們的標準json格式中。以下是我們如何做到的:

var url = "sendAnswer.json"; 
var questionId = 11; //This is set elsewhere 
var fileValue = // Need to get the image file here 
$.post(url,{ 
"data[Answer][question_id]": questionId, 
"data[Answer][value]" : fileValue 
}); 

如何正確設置fileupload中的選項以匹配我需要發送的內容?基本上我需要將上傳文件放入數據數組中。默認情況下,我相信它是由插件單獨處理的。

回答

0

首先要以正確的格式的對象,然後通過該對象:

var url = "sendAnswer.json"; 
var questionId = 11; //This is set elsewhere 
var fileValue = // Need to get the image file here 
dataObj = {"Answer": {"question_id": questionId, "value": fileValue}}; 
$.post(url,{ 
"data": dataObj 
}); 
+0

感謝這一點,但我的主要問題是如何讓插件處理的圖像文件設置爲我可以在數組中傳遞的對象。在這種情況下,將其設置爲'var fileValue' – Voodoo

+0

git自述文件中有大量文檔和演示:https://github.com/blueimp/jQuery-File-Upload/wiki/Setup – Jade

0

添加一個答案,我問的方式回到了問題的情況下,有人試圖做類似的事情。您可以通過回調函數上的data.files獲取圖像細節(比如「add」)。

var theUrl = 'url here'; 
$('#fileupload').fileupload({ 
     url: theUrl, 
     dataType: 'json', 
     autoUpload: false, 
}).on('fileuploadadd', function (e, data) { 
     // Can do things with the data here, "files" has details about the image 
     console.log(data.files); 
}) 
相關問題