Using iFrame to upload files through Ajax

Source: Internet
Author: User
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 ("

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.