Recently learned the C # implementation file upload and download, now share to everyone.
1. C # File Upload
Create a myupload.htm page for testing
<form name= "Form1" method= "Post" action= "uploadfile.aspx" id= "Form1" enctype= "Multipart/form-data"
>
<input type= "File" id= "txtfile" name= "Picfile"/></br> "<input type="
submit "value=" Upload "/>
Create the Uploadfile.aspx file and type the following code in UploadFile.aspx.cs:
Random rnd = new Random (); Generate random number private string _directory = @ "/file/uploadfile";
Directory protected void Page_Load (object sender, EventArgs e) {try {if (Requestfilescount > 0) {
Determine file size int length = Requestfiles[0]contentlength;
if (length > 1048576) {responsewrite ("file is greater than 1M, cannot be uploaded");
Return
} String type = Requestfiles[0]contenttype;
String fileext = Pathgetextension (requestfiles[0]filename) ToLower ();
can only upload pictures, filter the file type can not be uploaded string filefilt = "Gif|jpg|php|jsp|jpeg|png|"; if (Filefiltindexof (Fileext) <=-1) {Responsewrite ("Sorry!)" Please upload pictures!!
");
Return else {string fileName = Servermappath (_directory) + "\" + datetimenowtostring ("YYYYMMDDHHMMSSFF
F ") + Rndnext (+) ToString () + Fileext;
Requestfiles[0]saveas (FileName); Responsewrite ("Upload success!")
"); catch {throW new Exception ();
}
}
2. C # File download
To create the downloadfile.aspx, type the following method DownloadFile.aspx.cs:
<summary>///C # file download///</summary>///<param name= "filename" ></param> public void Mydow
Nload (string filename) {string path = Servermappath ("/file/" +filename); if (! FileExists (Path)) {Responsewrite ("Sorry!)" File does not exist!!
");
Return
} systemiofileinfo File = new Systemiofileinfo (path); String filefilt= "asp|aspx|php|jsp|ascx|config|asa|";
Non-downloadable files, be sure to filter the clean string filename = filename;
String fileext = Filenamesubstring (Filenamelastindexof ("")) Trim () ToLower (); if (Filefiltindexof (fileext)!=-1) {responsewrite ("Sorry!)" This class file is not allowed to download!!
");
else {responseclear (); Responseaddheader ("Content-disposition", "attachment;
Filename= "+ httputilityurlencode (fileName));
Responseaddheader ("Content-length", filelengthtostring ());
Responsecontenttype = getContentType (Httputilityurlencode (Fileext));
Responsewritefile (Filefullname);
Responseend (); }///<summary>///get download type///</summary>///<param name= "Fileext" ></param>///<returns></returns> public string Getcontenttyp
E (String fileext) {string ContentType;
Switch (fileext) {case "asf": ContentType = "video/x-ms-asf"; Case "avi": ContentType = "Video/avi";
Break Case "Doc": ContentType = "Application/msword";
Break Case "Zip": ContentType = "Application/zip";
Break Case "xls": ContentType = "Application/vndms-excel";
Break Case "gif": ContentType = "Image/gif";
Break Case "jpg": ContentType = "Image/jpeg";
Break Case "JPEG": ContentType = "Image/jpeg";
Break Case "wav": ContentType = "Audio/wav";
Break Case "MP3": ContentType = "Audio/mpeg3";
Break Case "mpg": ContentType = "Video/mpeg";
Break Case "MEPG": ContentType = "Video/mpeg";
Break Case "RTF": ContentType = "Application/rtf";
Break CasE "html": ContentType = "text/html";
Break Case "htm": ContentType = "text/html";
Break Case "TXT": ContentType = "Text/plain";
Break
Default:contenttype = "Application/octet-stream";
Break
return ContentType; }
* How to get the ContentType property of an existing file
<summary>
///Get contenttype properties of existing Files
///</summary>
///<param name= "filename" >< /param>
///<returns></returns> public
string Getfilecontenttype (string filename)
{
string[] Array = filenamesplit (');
string result = Stringempty;
string suffix = "" + array[arraylength-1];
Microsoftwinregistrykey RG = microsoftwinregistryclassesrootopensubkey (suffix);
Object obj = Rggetvalue ("Content Type");
result = obj!= null? Objtostring (): Stringempty;
Rgclose ();
return result;
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.