ASP. NET upload and download files

Source: Internet
Author: User

ASP. NET upload and download files

Upload files:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.IO; 
# Region upload files
/// <Summary> /// upload a file /// </Summary> /// <Param name = "uploadfilepath"> upload location </param> /// <Param name = "strtime"> current time (add time to rename the file, prevent duplicates) </param> // <Param name = "Upload"> upload Control </param> private void uploadmethod (string uploadfilepath, string strtime, fileupload upload) {bool fileok = false; // File Upload path string Path = server. mappath (uploadfilepath); // determines whether the upload folder exists. If not, create if (! Directory. exists (PATH) {// create a folder directory. createdirectory (PATH);} // if a file is selected, run if (upload. hasfile) {// obtain the type of the uploaded file string fileextesion = system. io. path. getextension (upload. filename ). tolower (); // get the file name without the extension string result = system. io. path. getfilenamewithoutextension (upload. filename); // string [] allowextesions = {". doc ",". xls ",". RAR ",". zip ",". ppt "}; For (INT I = 0; I <allowextesions. l Ength; I ++) {If (fileextesion = allowextesions [I]) {// the file format is correct. Allow uploading fileok = true;} If (fileok) {try {string newname = Result + strtime + fileextesion; upload. postedfile. saveas (path + newname); Session ["FILENAME"] = newname;} catch (exception) {page. clientscript. registerstartupscript (page. getType (), "message", "<script language = 'javascript 'defer> alert ('upload failed! '); </SCRIPT> ") ;}} else {page. clientscript. registerstartupscript (page. getType (), "message", "<script language = 'javascript 'defer> alert ('upload failed! '); </SCRIPT> ") ;}} else {// file page not selected. clientscript. registerstartupscript (page. getType (), "message", "<script language = 'javascript 'defer> alert (' No file has been selected. Please select a file! '); </SCRIPT> "); Return ;}# endregion

Download file:

   using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.IO;using System.Text.RegularExpressions;
/// <Summary> /// download the object /// </Summary> /// <Param name = "downloadfilepath"> file path </param> private void downloadmethod (string downloadfilepath) {string Path = server. mappath (downloadfilepath); RegEx Re = new RegEx (@ "\ W *\. \ W * "); string ST = Re. match (PATH ). value. tostring (); string filename = sT; // file name saved by the Client: fileinfo = new fileinfo (PATH); response. clear (); response. clearcontent (); response. clearheaders (); response. addheader ("content-disposition", "attachment; filename =" + filename); response. addheader ("Content-Length", fileinfo. length. tostring (); response. addheader ("content-transfer-encoding", "binary"); response. contenttype = "application/octet-stream"; response. contentencoding = system. text. encoding. getencoding ("gb2312"); response. writefile (fileinfo. fullname); response. flush (); response. end ();}

Call:

Uploadmethod (docpath, strtime, fileupload );

Downloadmethod (filepath );

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.