Asp.net uses the new features of H5 to Implement Asynchronous upload. New Features of h5

Source: Internet
Author: User

Asp.net uses the new features of H5 to Implement Asynchronous upload. New Features of h5

###Index.html

<! DOCTYPE html> 


### Index. js

Function formDataUpload () {// You can select multiple files at a time. var fileUpload = document. getElementById ("FileToUpload "). files; if (fileUpload. length = 0) {alert ("select a file and upload it again"); return;} // new html5 feature var formdata = new FormData (); // Add the uploaded data for (var I = 0; I <fileUpload. length; I ++) {formdata. append ('files', fileUpload [I]);} // use javascript native ajax var xmlHttp = new XMLHttpRequest (); xmlHttp. open ("post", 'handler. ashx? Method = formDataUpload '); xmlHttp. onreadystatechange = function () {if (xmlHttp. readyState = 4 & xmlHttp. status = 200) {alert ("uploaded successfully") ;}} xmlHttp. send (formdata );}

### Handler. ashx

<% @ WebHandler Language = "C #" Class = "Handler" %> using System; using System. web; public class Handler: IHttpHandler {public void ProcessRequest (HttpContext context) {formDataUpload (context);} public static void formDataUpload (HttpContext context) {// obtain the file HttpFileCollection files = context submitted by the client. request. files; string msg = string. empty; string error = string. empty; int fileM = 0; if (files. count> 0) {for (int I = 0; I <files. count; I ++) {; String path = @ "D: \" + files [I]. fileName; files [I]. saveAs (path); fileM + = files [I]. contentLength;} msg = "uploaded successfully, total file size:" + fileM; string res = "{error: '" + error + "', msg: '"+ msg +"'} "; context. response. write (res); context. response. end () ;}} public bool IsReusable {get {return false ;}}}

The above example of asp.net using the new H5 feature to Implement Asynchronous upload is all the content shared by Alibaba Cloud. I hope to give you a reference, and I hope you can provide more support to the customer's house.

Related Article

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.