[英]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对象的
name
, size
和type
属性。
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.