asp.net擷取HTML表單File中的路徑的方法

來源:互聯網
上載者:User

 這篇文章主要介紹了asp.net怎麼擷取HTML表單File中的路徑,需要的朋友可以參考下

 代碼如下:#region 上傳檔案到資料庫和伺服器 public void FN_UpFiles() { //遍曆File表單元素 HttpFileCollection files = HttpContext.Current.Request.Files; try { for (int iFile = 0; iFile < files.Count; iFile++) { //檢查副檔名字 HttpPostedFile postedFile = files[iFile]; string fileName = "";//定義檔案名稱 //string fileExtension = ""; fileName = Path.GetFileName(postedFile.FileName);//得到上傳檔案的完整名稱 即檔案名稱+尾碼名 int index = fileName.IndexOf("."); string FileType = fileName.Substring(index).ToLower();//截取檔案尾碼名 //FileTypeImg = "../FileTypeimg/" + hz + ".gif"; Guid fileGuid = Guid.NewGuid();//產生新的檔案名稱 以GUID命名防止檔案名稱相同 string NewFileName = fileGuid.ToString();//新的檔案名稱 NewFileName = NewFileName + FileType;//新的檔案名稱+尾碼名 if (postedFile.ContentLength > 2097151 * 1024)//判斷是否大於設定檔中的上傳檔案大小 { Page.RegisterStartupScript("提示", "<script language='javascript'>alert('對不起您的上傳資源過大!');return;</script>"); return; } else { if (fileName != "")//如果檔案名稱不為空白 { try { //檔案虛擬路徑 string strpath = System.Web.HttpContext.Current.Server.MapPath("~/Upload/") + NewFileName; try { NRModel.File model = new NRModel.File(); NRBLL.File bf = new NRBLL.File(); Guid guid1 = Guid.NewGuid(); Guid guid2 = new Guid(FolderId); Guid guid3 = Guid.NewGuid(); Guid guid4 = Guid.NewGuid(); model.Fileid = guid1; model.Folderid = guid2; model.Filepath = strpath; model.FileNam = fileName; model.FileSize = postedFile.ContentLength; model.Decription = TextArea1.Value.ToString(); model.CreateOn = DateTime.Now; model.CreateBy = guid3; model.ModefyBy = guid4; if (bf.FN_AddNewRes(model) > 0) { NR.Error.Log.LogType("上傳資源" + fileName + "成功!" + "伺服器路徑:" + strpath); //儲存檔案到指定目錄(虛擬目錄) postedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("~/Upload/") + NewFileName); //Page.RegisterStartupScript("提示", "<script language='javascript'>alert('上傳成功!');self.opener.location.reload();window.close();</script>"); AlertMsg("上傳成功!"); } } catch (Exception ex) { NR.Error.Log.LogType(ex.ToString()); }   } catch (Exception ex) { NR.Error.Log.LogType(ex.ToString()); } } else { Response.Write("上傳檔案不可為空!"); NR.Error.Log.LogType("檔案不可為空!"); } }   } } catch (System.Exception ex) { NR.Error.Log.LogType(ex.ToString()); } } #endregion  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.