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 );