Some time ago, I struggled for a while to solve the problem of ajax uploading files. There is definitely no problem in uploading text information directly using $.post. But it is not feasible to upload images directly in $.post.
Later I saw some solutions on the Internet, including ready-made ajax uploaded file encapsulation methods and some using flash. Flash is indeed a good method, but not everyone knows flash, and it is not easy to modify the downloaded ready-made method, and the file is relatively large. In the end, I had to simulate iframe to achieve it. Found it to be quite simple.
html:
<iframe name="ajaxUpload" style="display:none"></iframe> <form name="from1" id="from1" method="post" action="url" enctype="multipart/form-data"target="ajaxUpload"> <table> <tr> <td>附件:</td> <td><input type="file" id="document" name="document"/></td> </tr> </table> </form>
Here is the key point. To upload files, the enctype attribute is indispensable. The value of target is changed to the value of iframe's name.
Write the js code below. I use jQuery, so it is essential to load the jquery library when using it.
$(function(){ if($.browser.msie){ window.form1.submit();}else{ $("#form1").submit();} });
Here is a browser version judgment, because IE is a browser that does not meet the specifications, especially IE6. IE6 does not directly support $("#idName").submit();.
The server is as follows, it has to return a value, direct submission cannot return a value
public void Upload() { HttpPostedBase ff=Request.Files["document"];//这里是获取上传的文件流,也可以用索引值来表示如果是多个文件的话 string fileName=System.DateTime.Now+ff.FileName.ToString(); //这里取出来的文件名是没有后缀的,所以要保存的话还需要取出文件拓展名。这里就不写过细,只是为描述这样一个思路。 try { SaveAs(documentPath+fileName+extendtionName); Response.Write("<script type='text/javascript' type='language'>parent.window.callBackMethod('上传成功');</scrpt>"); } catch { Response.Write("<script type='text/javascript' type='language'>parent.window.callBackMethod('上传失败');</scrpt>"); //parent.window.methodName();这个是JS调用父页的方法。因为现在模拟一个iframe上传文件,这个iframe的作用就是一个中间站的作用。在父页点击上传后通过target会将页面文档流传入iframe中再上传服务端作处理。服务端有响应之后然后再在iframe里面显示出来,而不是直接在父页面显示出结果。这里可能就是一个alert()弹出一个对话框提示一下,如果是这样那么不调父页方法也行。如果想把这提示的内容丰富一点比如弹出个类似人人网的蓝色的对话框之类的。 } }
Simulating an iframe is actually a partial update of the page, but the iframe in the page has no content and is not displayed, so it Refreshing will not affect the entire page at all.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

WebStorm Mac version
Useful JavaScript development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Notepad++7.3.1
Easy-to-use and free code editor
