本文實例講述了C#實現(xiàn)的文件上傳下載工具類。分享給大家供大家參考,具體如下:
崇左網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站設(shè)計等網(wǎng)站項目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)于2013年開始到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運(yùn)維經(jīng)驗,來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。
這里給出的工具類是在VS2013環(huán)境下采用C#語言實現(xiàn)文件上傳、下載功能。上傳時,為避免文件名在服務(wù)器中重復(fù),采用“服務(wù)器時間+8位隨機(jī)碼+文件名+文件后綴“的方式作為服務(wù)器上的文件名;下載采用的是WebAPI的方式進(jìn)行的,下載成功后可自定義文件的保存路徑。
具體源碼如下所示:
using System; using System.IO; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using JYRS.Util; namespace JYRS.Utils { public class FileHelper { /// <summary> /// 將文件名解析成文件的上傳路徑 /// </summary> /// <param name="fileName">文件名</param> /// <param name="path">文件路徑</param> /// <returns path>文件在服務(wù)器上的路徑</returns> public static String transPath(string fileName, string path) { createDir(path); //取服務(wù)器時間+8位隨機(jī)碼作為部分文件名,確保文件名無重復(fù) string nowStr = DateTime.Now.ToString("yyyyMMddhhmmssff") + Global.CreateRandomCode(8); // 去掉后綴的文件名 string fileNameStr = fileName.Substring(0, fileName.LastIndexOf(".")); // 文件后綴 String suffix = fileName.Substring(fileName.LastIndexOf(".") + 1); if (fileName.Trim() != "") { // 如果名稱不為"",說明該文件存在,否則說明該文件不存在 path += "\\" + fileNameStr + nowStr + "." + suffix;// 定義上傳路徑 } return path; } /// <summary> /// 創(chuàng)建文件目錄 /// </summary> /// <param name="root">根目錄</param> /// <returns ></returns> private static void createDir(String root) { // 檢查目錄 if (!Directory.Exists(System.Web.HttpContext.Current.Server.MapPath(root))) { Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(root)); } } /// <summary> /// 根據(jù)文件在服務(wù)器上的路徑下載文件,此處采用的是WebAPI的方式進(jìn)行文件下載,下載成功后可自定義文件的保存路徑 /// </summary> /// <param name="fileName">文件名</param> /// <param name="path">文件路徑</param> /// <returns></returns> public static HttpResponseMessage download(string fileName, string path) { try { var stream = new FileStream(path, FileMode.Open); HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new StreamContent(stream); response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = fileName }; return response; } catch { return new HttpResponseMessage(HttpStatusCode.NoContent); } } } }
Controller層調(diào)用類
[HttpGet] public HttpResponseMessage UploadAndDownload() { //文件上傳到服務(wù)器上的根目錄 string root = System.Web.Hosting.HostingEnvironment.MapPath(@"~/upload"); string fileName = "測試.docx"; //解析文件在服務(wù)器上的上傳路徑 string path = FileHelper.transPath(fileName, root); //獲取要上傳的文件 var files = HttpContext.Current.Request.Files; HttpPostedFile file = HttpContext.Current.Request.Files[0]; //保存文件 file.SaveAs(System.Web.HttpContext.Current.Server.MapPath(path)); //下載word文件 return FileHelper.download(fileName, path); }
view層:
<a href="http://localhost:60179/api/CeshiController/UploadAndDownload" rel="external nofollow" "> 導(dǎo)出 </a>
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#文件操作常用技巧匯總》、《C#遍歷算法與技巧總結(jié)》、《C#程序設(shè)計之線程使用技巧總結(jié)》、《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》及《C#面向?qū)ο蟪绦蛟O(shè)計入門教程》
希望本文所述對大家C#程序設(shè)計有所幫助。
新聞名稱:C#實現(xiàn)的文件上傳下載工具類完整實例【上傳文件自動命名】
分享URL:http://aaarwkj.com/article14/ijpjge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、云服務(wù)器、營銷型網(wǎng)站建設(shè)、動態(tài)網(wǎng)站、企業(yè)建站、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)