Step by step -70-asp.net simple Exercises (file upload and download)

Source: Internet
Author: User
Tags httpcontext ftp protocol

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.html

1.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.ashx

2 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; }        }    }}
ashx

2.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>HTML

3 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; }        }    }}
ashx

Step by step -70-asp.net simple Exercises (file upload and 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.