Some time ago, I had a hard time uploading files through ajax. Use $. post to upload text information.
Some time ago, I had a hard time uploading files through ajax. It is certainly no problem to directly upload text information using $. post. However, direct upload of images through $. post is not feasible.
Later, I saw some solutions on the Internet, including the methods for encapsulating files uploaded via ajax and using flash. Flash is indeed a good method, but not everyone will flash, and it is not easy to download the ready-made method for modification, and the file is relatively large. Finally, we had to simulate iframe for implementation. The discovery is quite simple.
Html:
Here is the focus. The enctype attribute of the file to be uploaded is indispensable. The target value is changed to the value of iframe name.
Below I will write the js Code. 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 non-compliant browser, especially IE6. IE6 does not directly support $ ("# idName"). submit.
If the server side is as follows, a value must be returned. Direct submit cannot return the value.
Public void Upload () {HttpPostedBase ff = Request. files ["document"]; // This is the stream of the uploaded file. You can also use the index value to indicate that if multiple Files exist, string fileName = System. dateTime. now + ff. fileName. toString (); // The obtained file name does not have a suffix. To save the file, you need to retrieve the extension name. I will not write too much here, just to describe this idea. Try {SaveAs (documentPath + fileName + extendtionName); Response. Write ("