How ajax uploads files and how ajax uploads files

Source: Internet
Author: User

How ajax uploads files and how ajax uploads files

Ajax is already common, but it is not involved in uploading files using ajax. I only know that it is complicated. Uploading files is different from uploading normal form data and must be specially processed, I Know That ajax uses the upload function to apply a virtual iframe, but I found it on the internet due to project requirements. I did not expect to find two jquery plug-ins that support ajax file upload, jquery. uploadify. js and dropzone. min. both js and ajax can be used to upload files, and the effect is good.


1. UseUpload files using jquery. uploadify. js

 This upload method uses Flash, so you need to reference a special js file swfobject. js, the earlier version needs to be referenced. The later version of this file is already included in jquery. uploadify. js is in progress. In addition, the parameters of earlier versions and later versions are not the same.

Complete foreground code using Uploadify v3.2.1

<Html xmlns = "http://www.w3.org/1999/xhtml"> 


Uploadify v2.1.0

Because swfobject. js is not available, you need to add references.

    <script src="Scripts/swfobject.js" type="text/javascript"></script>
Other parameters are also different, such as uploader and script

Complete front-end code

<Html xmlns = "http://www.w3.org/1999/xhtml"> 

2. Use dropzone. min. js to upload files

Flash is not used, But browser requirements are relatively high. For example, IE10 or above is supported. html5 technology should be used.

Complete front-end code,

<Html xmlns = "http://www.w3.org/1999/xhtml"> 

3. background processing code

Public class Handler1: IHttpHandler {public void ProcessRequest (HttpContext context) {string operatype = context. request. queryString ["OperationType"]; if (operatype = "upfile") {HttpPostedFile file = context. request. files ["FileData"]; if (file! = Null) {string fullPath = System. IO. path. combine (System. IO. path. combine (AppDomain. currentDomain. baseDirectory, "update"), DateTime. now. date. toString ("yyyyMMdd"); file. saveAs (fullPath + file. fileName) ;}} context. response. contentType = "text/plain"; context. response. write ("uploaded successfully");} public bool IsReusable {get {return false ;}}}



Source code download



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.