简体   繁体   English

使用javascript和jquery从表单的输入文件中上传文件数据

[英]Upload file data from input of type file in a form using javascript and jquery

I have a from with input of type file I need to get the file data from this form without refreshing the page I'm try to use this function 我有一个from类型文件输入,我需要从此表单获取文件数据而不刷新页面,我尝试使用此功能

function submitForm(form){
  var url = $(form).attr("action");
  var formData = {};
  $(form).find("input[name]").each(function (index, node) {
     formData[node.name] = node.value;
  });
  $.post(url, formData).done(function (data) {
     alert(data);
  }); 
}

but this function get the values of form inputs, but I need to get all file data (tmp_name, file_name, file_type ...) 但是此函数获取表单输入的值,但是我需要获取所有文件数据(tmp_name,file_name,file_type ...)
so can any one help me in this please 所以有人可以帮我吗
thanks in advance 提前致谢

Maybe you can reference your input of type file by id and then get the files property to obtain information about the files. 也许您可以通过id来引用类型为file的输入,然后获取files属性以获取有关文件的信息。 Then you can loop through the files and for example read the name , size and type attribute of the File object. 然后,您可以循环浏览文件,例如读取File对象的namesizetype属性。

https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications https://developer.mozilla.org/en-US/docs/Web/API/File https://developer.mozilla.org/zh-CN/docs/Using_files_from_web_applications https://developer.mozilla.org/zh-CN/docs/Web/API/File

For example 例如

 $("#theForm").submit(function(e) { submitForm(this); e.preventDefault(); }); function submitForm(form) { var url = $(form).attr("action"); var formData = {}; formData.filesInfo = []; var files = $('#inputFile').prop("files"); $(files).each(function() { var fileInfo = {}; fileInfo.name = this.name; fileInfo.size = this.size; fileInfo.type = this.type; formData.filesInfo.push(fileInfo); }); $.post(url, formData).done(function (data) { alert(data); }); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="theForm"> <input type="file" id="inputFile"> <input type="submit" name="submit" id="submit"> </form> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用输入类型作为文件从浏览器将图像与javascript的json数据一起上传到服务器? - How to upload an image from the browser using input type as file to the server along with json data using javascript? 使用html5 / ajax / javascript上传/发布文件,而不使用表单和<input type=file> - upload / POST file with html5 / ajax / javascript without using form and <input type=file> 如何清除文件 <input type='file' /> 使用jQuery或JavaScript - How to clear files from <input type='file' /> using jQuery or JavaScript 如何从 curl -T --upload-data 选项获取等效数据格式<input type="file"/>在浏览器中使用 JavaScript? - How to get the equivalent data format of curl -T --upload-data option from <input type="file"/> using JavaScript in browser? 想要使用javascript / jquery在输入标签中显示文件上传大小 - want to show file upload size in input tag using javascript/jquery 使用 javascript 从文件上传表单访问文件名 - Accessing file names from a file upload form using javascript 选择文件浏览器不适用于使用Jquery或javascript的输入类型文件 - select file browser is not working for input type file using Jquery or javascript 克隆文件上传输入以jQuery形式提交 - Clone file upload input filed in form with jquery php javascript表单上传文件验证<input> - php javascript form validation for upload file <input> 通过ajax post(jQuery)发送输入类型=“文件”表单数据 - Send input type=“file” form data via ajax post (jQuery)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM