How to send additional data to jQuery file upload plugin?

This may sound silly, but I can't understand the documentation. I am talking about this plugin to download a file.

Now according to the documentation there is an option:

FormData STRONG>

Additional form data that must be sent along with the file upload 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 downloading XHR files), or a simple object. The form of the first fileInput is set as a function parameter.

Note : Additional form data is ignored if the multipart parameter is set to false.

Type: Array, Object, function or FormData Default value: function returning form fields as serialized arrays:

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

Example:

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

I donโ€™t understand what I like to do.

This is how I initialize the plugin:

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

And this is my attempt to 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; } 
+4
source share
2 answers

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

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

Then, to add additional options

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

Example:

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

Changing thedate with what you want to call a parameter

Although it sounds like a simple object should work fine

  //name:value formData : {thedate:getDate} 
+7
source

Do you have multipart set to false in your form? Also make sure that the format of what you are sending back is acceptable.

Try hard coding the next line and send back the information:

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

Source: https://habr.com/ru/post/1441594/


All Articles