使用Blueimp jQuery文件上传插件向JSON添加文件

使用Blueimp jQuery文件上传插件向JSON添加文件,jquery,ajax,file,upload,Jquery,Ajax,File,Upload,我正在开发一种新型调查问题的前端,它允许用户上传图像文件。鉴于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[

我正在开发一种新型调查问题的前端,它允许用户上传图像文件。鉴于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中的选项以匹配需要发送的内容?基本上我需要把上传文件放到数据数组中。默认情况下,我相信它是由插件单独处理的。

首先创建具有正确格式的对象,然后传递该对象:

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
});

为我以前问过的问题添加一个答案,以防有人试图做类似的事情。您可以通过add等回调上的data.files获取图像详细信息

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);
})

谢谢,但我的主要问题是如何将插件处理的图像文件设置为可以在数组中传递的对象。在本例中,将其设置为var filevaluegit自述文件上有大量文档和演示: