javascript - FormData add file doesn't work -


i have form:

<form enctype="multipart/form-data" action="" method="post"  id="sendinvoiceform">     <input type="text" value="some text">     <input name="file[]" type="file"  multiple/>     <input type="button" id="upload" value="upload file" /> </form> 

my js:

$('#upload').click(function(e) {             e.preventdefault();             var formdata = new formdata();             formdata.append('files',$("#sendinvoiceform")[0]);             $.ajax({                 url: 'upload.php',                 type: 'post',                 xhr: function() {                     var myxhr = $.ajaxsettings.xhr();                     return myxhr;                 },                 success: function (data) {                 },                 data: formdata,                 cache: false,                 contenttype: false,                 processdata: false             });             return false;         }); 

when try 'files' in php [object htmlformelement] how can files on php?

but if create formdata :var formdata = new formdata($("#sendinvoiceform")[0]); can find files in _files, need give name array.

how can solve problem? thanks

the issue because you're appending form dom element formdata, not file data. instead, should access files array of object:

formdata.append('files', $('#sendinvoiceform input[type="file"]')[0].files[0]); 

as there can multiple files selected, you'll need loop through them:

$('#sendinvoiceform input[type="file"]')[0].files.foreach(function(file) {   formdata.append('files', file, file.name); }); 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -