C#實(shí)現(xiàn)文件上傳與下載功能實(shí)例
最近學(xué)習(xí)了 C#實(shí)現(xiàn)文件上傳與下載,現(xiàn)在分享給大家。
1、C#文件上傳
創(chuàng)建MyUpload.htm頁面,用于測試
<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="上傳" /> </form>
創(chuàng)建UploadFile.aspx文件,在UploadFile.aspx.cs鍵入如下代碼:
Random rnd = new Random(); //產(chǎn)生隨機(jī)數(shù) private string _directory = @"/File/UploadFile"; //目錄 protected void Page_Load(object sender, EventArgs e) { try { if (RequestFilesCount > 0) { //判斷文件大小 int length = RequestFiles[0]ContentLength; if (length > 1048576) { ResponseWrite("文件大于1M,不能上傳"); return; } string type = RequestFiles[0]ContentType; string fileExt = PathGetExtension(RequestFiles[0]FileName)ToLower(); //只能上傳圖片,過濾不可上傳的文件類型 string fileFilt = "gif|jpg|php|jsp|jpeg|png|"; if (fileFiltIndexOf(fileExt) <= -1) { ResponseWrite("對不起!請上傳圖片??!"); return; } else { string fileName = ServerMapPath(_directory) + "\\" + DateTimeNowToString("yyyyMMddHHmmssfff") + rndNext(10, 99)ToString() + fileExt; RequestFiles[0]SaveAs(fileName); ResponseWrite("上傳成功!"); } } } catch { throw new Exception(); } }
2 、C#文件下載
創(chuàng)建DownloadFile.aspx,在DownloadFile.aspx.cs鍵入如下方法:
/// <summary> /// C#文件下載 /// </summary> /// <param name="filename"></param> public void MyDownload(string filename) { string path = ServerMapPath("/File/"+filename); if(!FileExists(path)) { ResponseWrite("對不起!文件不存在!!"); return; } SystemIOFileInfo file = new SystemIOFileInfo(path); string fileFilt="asp|aspx|php|jsp|ascx|config|asa|"; //不可下載的文件,務(wù)必要過濾干凈 string fileName = fileName; string fileExt = fileNameSubstring(filenameLastIndexOf(""))Trim()ToLower(); if(fileFiltIndexOf(fileExt)!=-1) { ResponseWrite("對不起!該類文件禁止下載??!"); } else { ResponseClear(); ResponseAddHeader("Content-Disposition", "attachment; filename=" + HttpUtilityUrlEncode(fileName)); ResponseAddHeader("Content-Length", fileLengthToString()); ResponseContentType = GetContentType(HttpUtilityUrlEncode(fileExt)); ResponseWriteFile(fileFullName); ResponseEnd(); } } /// <summary> /// 獲取下載類型 /// </summary> /// <param name="fileExt"></param> /// <returns></returns> public string GetContentType(string fileExt) { string ContentType; switch (fileExt) { case "asf": ContentType = "video/x-ms-asf"; break; 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; }
*如何獲取現(xiàn)有文件的ContentType屬性
/// <summary> /// 獲取現(xiàn)有文件的ContentType屬性 /// </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; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實(shí)現(xiàn)DataTable,List和Json轉(zhuǎn)換的方法
這篇文章主要介紹了C#實(shí)現(xiàn)DataTable,List和Json轉(zhuǎn)換的方法,結(jié)合實(shí)例形式分析了DataTable、list、DataReader、DataSet等轉(zhuǎn)換成JSON的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-08-08解決C#程序只允許運(yùn)行一個(gè)實(shí)例的幾種方法詳解
本篇文章是對C#中程序只允許運(yùn)行一個(gè)實(shí)例的幾種方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C#中WPF ListView綁定數(shù)據(jù)的實(shí)例詳解
這篇文章主要介紹了C#中WPF ListView綁定數(shù)據(jù)的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-10-10