Uploading large files is usually done through the FTP protocol, and the general small files can be completed via the HTTP protocol.
1 uploading images via ASP.
1.1 Creating HTML pages
Note: 1 method= "POST"; 2 enctype= "Multipart/form-data"; 3 <input type= "file"/>
<! DOCTYPE html>"http://www.w3.org/1999/xhtml">"Content-type"Content="text/html; Charset=utf-8"/> <title></title>"Post"action="fileupload.ashx"Enctype="Multipart/form-data"> <input type="file"Id="Imgupload"Name="Imgupload"/> <input type="Submit"Value="Submit"/> </form></body>fileupload.html1.2 Create a generic handler. ashx
Note: 1 Create file Save path
usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingsystem.web;namespaceupload and download of _05_ file {/// <summary> ///Summary description of FileUpload/// </summary> Public classFileupload:ihttphandler { Public voidProcessRequest (HttpContext context) {context. Response.ContentType="text/html"; //01 getting FilesHttppostedfile PF = context. request.files["Imgupload"]; //02 Creating a File Save path stringSavepath = Path.Combine (appdomain.currentdomain.basedirectory+"upload/"+PF. FileName); //03 Saving FilesPF. SaveAs (Savepath); //04 displaying the uploaded fileContext. Response.Write (""+PF. filename+"'/>"); } Public BOOLisreusable {Get { return false; } } }}
fileupload.ashx2 Upload file format verification, assuming that the rules can only be uploaded, gif pictures
We can validate in HTML with jquery, or in. ashx
2.1 Modify ASHX File
usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingsystem.web;namespaceupload and download of _05_ file {/// <summary> ///Summary description of FileUpload/// </summary> Public classFileupload:ihttphandler { Public voidProcessRequest (HttpContext context) {context. Response.ContentType="text/html"; //01 getting FilesHttppostedfile PF = context. request.files["Imgupload"]; //01-01 get file suffix name stringExtname = PF. Filename.substring (PF. Filename.lastindexof ('.')); if(Extname! =". gif"|| Extname! =". Gif") {context. Response.Write ("please upload. gif pictures"); return; } //02 Creating a File Save path stringSavepath = Path.Combine (appdomain.currentdomain.basedirectory+"upload/"+PF. FileName); //03 Saving FilesPF. SaveAs (Savepath); //04 displaying the uploaded fileContext. Response.Write (""+PF. filename+"'/>"); } Public BOOLisreusable {Get { return false; } } }}
ashx2.2 Introducing jquery, modifying HTML pages
<! DOCTYPE html>"http://www.w3.org/1999/xhtml">"Content-type"Content="text/html; Charset=utf-8"/> <script src="Http://localhost:62225/Script/jquery-1.7.1.min.js"></script> <title></title> <script>$ (function () {$ ("form"). Submit (function () {varFName = $ ("#imgUpLoad"). Val (); varExtname = fname.substring (Fname.lastindexof ('.')); if(Extname! =". gif"|| Extname! =". Gif") {alert ("please upload. gif pictures"); return false; } }); }); </script>"Post"action="fileupload.ashx"Enctype="Multipart/form-data"> <input type="file"Id="Imgupload"Name="Imgupload"/> <input type="Submit"Value="Submit"/> </form></body>HTML3 If the file is only placed in the upload folder, with the increase in time, the file will inevitably become more and more detrimental to the search, can be based on the date to create the appropriate folder
usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingsystem.web;namespaceupload and download of _05_ file {/// <summary> ///Summary description of FileUpload/// </summary> Public classFileupload:ihttphandler { Public voidProcessRequest (HttpContext context) {context. Response.ContentType="text/html"; //01 getting FilesHttppostedfile PF = context. request.files["Imgupload"]; //01-01 get file suffix name stringExtname = PF. Filename.substring (PF. Filename.lastindexof ('.')); if(Extname! =". gif"&& Extname! =". GIF") {context. Response.Write ("please upload. gif pictures"); return; } //02 Creating a File Save path stringSavepath = Path.Combine (appdomain.currentdomain.basedirectory+"upload\\"); //02-01 Creating folders based on datesDateTime dt =DateTime.Now; Savepath+ = dt. Year +"\\"+ dt. Month +"\\"+dt. Day; if(!directory.exists (Savepath)) { //Create a folderdirectory.createdirectory (Savepath); } //02-02 file name is current timeSavepath+="\\"+ dt. ToString (). Replace (':','-')+". gif"; //03 Saving FilesPF. SaveAs (Savepath); //04 displaying the uploaded fileContext. Response.Write (""+ savepath.substring (Savepath.indexof ("Upload")) +"'/>"); } Public BOOLisreusable {Get { return false; } } }}
ashxStep by step -70-asp.net simple Exercises (file upload and download)