国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#實現的文件上傳下載工具類完整實例【上傳文件自動命名】

2019-10-29 21:07:25
字體:
來源:轉載
供稿:網友

本文實例講述了C#實現的文件上傳下載工具類。分享給大家供大家參考,具體如下:

這里給出的工具類是在VS2013環境下采用C#語言實現文件上傳、下載功能。上傳時,為避免文件名在服務器中重復,采用“服務器時間+8位隨機碼+文件名+文件后綴“的方式作為服務器上的文件名;下載采用的是WebAPI的方式進行的,下載成功后可自定義文件的保存路徑。

具體源碼如下所示:

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>文件在服務器上的路徑</returns>    public static String transPath(string fileName, string path)    {      createDir(path);      //取服務器時間+8位隨機碼作為部分文件名,確保文件名無重復      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>    /// 創建文件目錄    /// </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>    /// 根據文件在服務器上的路徑下載文件,此處采用的是WebAPI的方式進行文件下載,下載成功后可自定義文件的保存路徑    /// </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層調用類

[HttpGet]public HttpResponseMessage UploadAndDownload(){  //文件上傳到服務器上的根目錄  string root = System.Web.Hosting.HostingEnvironment.MapPath(@"~/upload");  string fileName = "測試.docx";  //解析文件在服務器上的上傳路徑  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" "> 導出 </a>

希望本文所述對大家C#程序設計有所幫助。


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 三原县| 武夷山市| 柘荣县| 类乌齐县| 天津市| 娄底市| 延庆县| 松桃| 华阴市| 汶上县| 喀什市| 元朗区| 乌鲁木齐市| 天峻县| 德令哈市| 昭觉县| 南木林县| 东阿县| 涡阳县| 木兰县| 榆林市| 津南区| 桂东县| 文昌市| 喀什市| 翁牛特旗| 绍兴市| 蓬莱市| 双牌县| 阜阳市| 祁门县| 多伦县| 石屏县| 揭阳市| 鲁山县| 嘉义市| 望江县| 白山市| 台中县| 河北区| 崇信县|