SERVER.MAPPATH


主要總結Server.MapPath 這個方法的使用以及使用的場景,不是什么時候都適合使用;
1、實現功能:
  Server.MapPath能夠獲取指定URL相對服務器的物理路徑,在IIS服務端,能夠根據文件名來獲取該文件的物理路徑;
2、存在命令空間:
  System.Web.HttpContext.Current.Server.MapPath 以及System.web.MVC.Control.Server.Mapth;
3、使用情況:
  既然是System.Web.HttpContent 也及時表明該方法只能放在Http.web中使用,非該環境系統會扔出一個錯誤;非web環境是什么意思那,舉個例子,我們使用線程來處理某個業務邏輯的時候,這個時候你使用該方法,那必然報錯,以為你已經脫離了web環境。所以視情況而定;獲取虛擬目錄的物理地址,該方法很有效果;
  隨便補充一句,多線程編程的時候,一定要分清楚那些事線程能夠獲取的資源,那些事依賴其他環境獲取的變量,比如IIS中多線程獲取緩存數據,離開了HttpWeb這環境來獲取IIS的緩存,必然是失敗的,所以要分清楚多線程編程時候,使用的資源對象。線程安全對象集合:ConcurrentQueue、ConcurrentBag等
4、需要注意事項:
    system.Web.HttpContext.Current.Server.MapPath("myPic") //也就是獲取當前平級目錄地址;
    system.Web.HttpContext.Current.Server.MapPath("../myPic") //也就是獲取當前上級目錄地址;
 
       使用的時候需要慎重;
SaveAs 方法將使用 FileUpload 控件上載的文件的內容保存到 Web 服務器上的指定路徑
獲取前端的頁面上的文件
 HttpFileCollection files = HttpContext.Current.Request.Files;
//原來以為這里的FILES只是file文件上傳才能取得,后來經測試發現這里的files只要是頁面上有的file文件(圖片等等),都會當做request.files傳過去
private void SaveFile(string xxbh, string flowLsh, string basePath = "~/Upload/Attachment/")
        {
            var _result = "0";
            DAL.DALBase dal = new DAL.DALBase();
            var name = string.Empty;
            var saveName = Guid.NewGuid().ToString().Replace("-", "");
            basePath = (basePath.IndexOf("~") > -1) ? System.Web.HttpContext.Current.Server.MapPath(basePath) : basePath;
            HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
            basePath += xxbh + "/";
            if (!Directory.Exists(basePath))
            {
                Directory.CreateDirectory(basePath);
            }
            dal.DB.BeginTransaction();
            try
            {
                var _suffix = files[0].FileName.Substring(files[0].FileName.LastIndexOf("."));
                var _temp = System.Web.HttpContext.Current.Request["name"];
                if (!string.IsNullOrEmpty(_temp))
                {
                    name = _temp;
                }
                else
                {
                    Random rand = new Random(24 * (int)DateTime.Now.Ticks);
                    name = rand.Next() + "." + _suffix;
                }
                var full = basePath + saveName;
                files[0].SaveAs(full);
                //保存數據庫
                string sqlString = "insert into t_xxbb_attachment\n" +
                "  (lsh, filename, filesuffix, filesize, ref_xxbh, scbz, sjc,ref_jgbh,ref_yhbh,ref_flow_lsh)\n" +
                "values\n" +
                "  ('" + saveName + "', '" + name + "', '" + _suffix + "', " + files[0].ContentLength + ", '" + xxbh +
                "', 0, sysdate," + UI.ssjgbh + "," + UI.yhbh + ",'" + flowLsh + "')";
                dal.DB.ExecuteNonQuery(sqlString);
                dal.DB.CommitTransaction();
                _result = "1";
            }
            catch (Exception ex)
            {
                dal.DB.RollbackTransaction();
                LogHelper.writelog("AttachmentUpload->SaveFile", ex);
            }
            System.Web.HttpContext.Current.Response.Write(_result);
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM