javascript 如何将附加数据发送到 jQuery 文件上传插件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13034468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:41:06  来源:igfitidea点击:

How to send additional data to jQuery File Upload Plugin?

javascriptjqueryjquery-plugins

提问by eric.itzhak

This would seem stupid but i can't seem to understand the documentation. I'm talking about Thisplugin for file upload.

这看起来很愚蠢,但我似乎无法理解文档。我说的是这个用于文件上传的插件。

Now according to the documentation there's an option :

现在根据文档有一个选项:

formData

Additional form data to be sent along with the file uploads can be set using this option, which accepts an array of objects with name and value properties, a function returning such an array, a FormData object (for XHR file uploads), or a simple object. The form of the first fileInput is given as parameter to the function.

Note: Additional form data is ignored when the multipart option is set to false.

Type: Array, Object, function or FormData Default: A function returning the form fields as serialized Array:

function (form) {
    return form.serializeArray();
}

Example:

[ { name: 'a', value: 1 }, { name: 'b', value: 2 } ]

表单数据

可以使用此选项设置要与文件上传一起发送的其他表单数据,该选项接受具有名称和值属性的对象数组、返回此类数组的函数、FormData 对象(用于 XHR 文件上传)或简单的目的。第一个 fileInput 的形式作为函数的参数给出。

注意:当 multipart 选项设置为 false 时,附加表单数据将被忽略。

类型:数组、对象、函数或 FormData 默认值:将表单字段作为序列化数组返回的函数:

function (form) {
    return form.serializeArray();
}

例子:

[ {名称:'a',值:1},{名称:'b',值:2}]

Which i fail to understand what i'm suppoused to do with that.

我不明白我应该用它做什么。

This is how i initialize the plugin :

这就是我初始化插件的方式:

$('#add_image_upload').fileupload({
            dataType: 'json',
    sequentialUploads: true,
    formData : getDate
});

And this is my attempt to the function :

这是我对功能的尝试:

 function getDate(){

//if user didn't selected a date
if(!selectedDate || selectedDate=="undefined"){
selectedDate = "1/1/"+$('#timeline').html();
}
var date= new Array(selectedDate);
return date;
}

回答by ?????

try turning your data into in object - with what they showed in their example

尝试将您的数据转换为对象 - 使用他们在示例中显示的内容

$('#add_image_upload').fileupload({
            dataType: 'json',
    sequentialUploads: true,
    formData : {name:'thedate',value:getDate}
});

Then to add more params

然后添加更多参数

           //name of param  // value
formData : [{name:'thedate',value:getDate},{name:'thedate2',value:'seconddate'},etc..]

Example:

[ { name: 'a', value: 1 }, { name: 'b', value: 2 } ]

例子:

[ {名称:'a',值:1},{名称:'b',值:2}]

Changing thedatewith whatever you want to name the param

更改thedate您想要命名参数的任何内容

Though it sounds like a simple object should work fine

虽然听起来像一个简单的对象应该可以正常工作

           //name:value
formData : {thedate:getDate}

回答by zeddotes

Do you have multipartset to falseon your form? Also, ensure the format of what you're sending back is acceptable.

你的表格上有multipart设置false吗?此外,请确保您发回的格式是可以接受的。

Try hard-coding the following line and sending back the info:

尝试对以下行进行硬编码并发回信息:

new dateobject = { "date": "1/1/2012" }