使用HttpRequest.Files 擷取上傳檔案,實現上傳附件功能

來源:互聯網
上載者:User

標籤:url   不同   creat   image   瀏覽器   led   des   error   ast   

使用HttpRequest.Files 擷取上傳檔案,實現上傳附件功能,不同瀏覽器會有差異:

  1. 獲得在 Google 瀏覽器上傳後得到的 HttpRequest.Files  (用戶端上傳檔案的集合)

                       

 

單個檔案查看:對應的FileName 是上傳檔案的原始檔案名:例:開發管理手冊2017版.docx

 

  1. 擷取IE瀏覽器上傳後HttpRequest.Files:

       

 

單個檔案查看:對應的FileName 是上傳檔案 帶路徑的檔案名稱 例:C:\\Users\\XXX\\Desktop\\開發管理手冊2017版.docx

         

 

  1. 因為不同瀏覽器對應的FileName不一致,在儲存檔案的時候,需要對FileName 進行處理:(僅擷取檔案名稱)

string aFirstName = aFile.Substring(postedFile.FileName.LastIndexOf("\\") + 1, (postedFile.FileName.LastIndexOf(".") - aFile.LastIndexOf("\\") - 1));

 

    [HttpPost, Route("PostFile")]        public ResponeResult<List<FileModel>> PostFile()        {            var result = new ResponeResult<List<FileModel>>(PromptCode.SUCCESS, "附件上傳成功。");            string[] allowExtension = { ".doc", ".docx", ".xls", ".xlsx", ".pdf", ".zip", ".rar", ".jpg", ".png", ".gif" };            var httpRequest = HttpContext.Current.Request;            List<FileModel> docfiles = new List<FileModel>();            if(httpRequest.Files.Count > 0) {                var date = DateTime.Now.ToString("yyyy-MM-dd");                var relativePath = string.Format("/Upload/Contract/{0}/", date);                var path = HttpContext.Current.Server.MapPath(@"~" + relativePath);                if(!Directory.Exists(path))//判斷是否存在                {                    Directory.CreateDirectory(path);//建立新路徑                }                FileModel mode = null;                foreach(string file in httpRequest.Files) {                    var postedFile = httpRequest.Files[file];                    string fileType = System.IO.Path.GetExtension(postedFile.FileName);                    if(string.IsNullOrEmpty(fileType)) {                        continue;                    }                    var rs = allowExtension.Contains(fileType.ToLower());                    if(!rs)                        continue;                    //對檔案名稱處理                     var aFile = postedFile.FileName;                    string aFirstName = aFile.Substring(postedFile.FileName.LastIndexOf("\\") + 1, (postedFile.FileName.LastIndexOf(".") - aFile.LastIndexOf("\\") - 1));                    string strGuid = Guid.NewGuid().ToString();                    //儲存檔案名稱                    var fileName = "file_" + strGuid + fileType;                    var filePath = path + fileName;                    postedFile.SaveAs(filePath);                    var webHostConfig = ConfigurationManager.AppSettings["WebHostDomain"];                    var webPath = string.Format("http://{0}{1}{2}", string.IsNullOrEmpty(webHostConfig) == true ? HttpContext.Current.Request.Url.Authority : webHostConfig, relativePath, fileName );                    mode = new FileModel();                    mode.FileId = strGuid;                    mode.FileName = aFirstName + fileType;                    mode.FilePath = filePath;                    mode.WebPath = webPath;                    mode.RelativePath = relativePath + fileName;                    mode.CreateTime = DateTime.Now;                    mode.CreateUser = Session.UserId;                    docfiles.Add(mode);                }                if(!this.fileService.BatchAdd(docfiles)) {                    return new ResponeResult<List<FileModel>>(PromptCode.ERROR, "上傳附件儲存失敗,請聯絡管理員。");                }                var filedata = docfiles;                result.Data = filedata;                return result;            }            else {                return new ResponeResult<List<FileModel>>(PromptCode.ERROR, "上傳附件為空白。");            }        }

 

使用HttpRequest.Files 擷取上傳檔案,實現上傳附件功能

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.