Ajax异步上传文件实例代码分享
时间:2021-09-15 10:43:37|栏目:JavaScript代码|点击: 次
非常不多说,直接给大家上干货,写的不好还请见谅。
具体代码如下所示:
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <script src="~/Scripts/jquery-1.8.2.min.js"></script> <title>Index</title> <script> $(function() { $(':button').click(function () { var formData = new FormData(); var file = $("input[type='file']")[0].files[0]; formData.append("files", file); $.ajax({ url: '/Upload/Upload', //server script to process data type: 'POST', //Ajax事件 //beforeSend: beforeSendHandler, //success: completeHandler, //error: errorHandler, // Form数据 data: formData, //Options to tell JQuery not to process data or worry about content-type cache: false, contentType: false, processData: false }); }); }); </script> </head> <body> <form enctype="multipart/form-data"> <input name="file" type="file" multiple="multiple" /> <input type="button" value="Upload" /> <input type="text" name="userName" value="ice" /> </form> </body> </html>