Asp.net

ASP.NET:System.UnauthorizedAccessException - 拒絕訪問路徑

  • February 28, 2015

我有一個 ASP.NET Web 應用程序,它執行以下操作:

  1. 讀取 Excel 文件。
  2. excel 文件中將有一個圖像 URL,該 URL 指向 Internet 上的某個位置。
  3. 該程序讀取每個圖像 URL 並將其儲存到 Web 伺服器的臨時文件夾中。
  4. 然後應用程序調整圖像的大小(更改寬度和高度)。
  5. 最後,應用程序將該圖像保存到另一個文件夾。

我收到以下異常:

System.Net.WebException:在 WebClient 請求期間發生異常。—> System.UnauthorizedAccessException: 拒絕訪問路徑 ‘\abcserver\target03\3111\35644\www.testing.com\web\content\images\TempStorage\tempImage.jpg’。在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 在 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs , String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access) at System.Net.WebClient.DownloadFile(Uri address, String fileName) — 內部異常堆棧跟踪結束—

在 ProcessImage.GetFileFromUrl(String imageFileUrl, String newFileName)

at uploadexceldata.UploadExcelData(String fileName)

foreach (DataRow dr in dt.Rows) // Reading each excel row
           {

               if (dr[0].ToString() != "")
               {
                   id= "";
                   path = "";
                   manuId = "";


                   id= dr[0].ToString();
                   path = dr[1].ToString();
                   fileNameOnly = iProImg.GetFileNameOnly(path);
                   objDb.openConnection();
                   strSqlGroupInfo = "select ManufacturerID from  manufacturers where id='" + id+ "'";

                   dTblManu = objDb.BuildDT(strSqlGroupInfo); // To Fill data to Datatable
                   objDb.closeConnection();
                   if (dTblManu.Rows.Count > 0)
                   {
                       manuId = dTblManu.Rows[0][0].ToString();
                   }

                   if (manuId != "")
                   {
                       tempUploadPath = "images/TempStorage/";
                       tempUploadPath = Server.MapPath(tempUploadPath);
                       if (!Directory.Exists(tempUploadPath))
                       {
                           Directory.CreateDirectory(tempUploadPath);
                       }
                       tempFilePath = tempUploadPath + "\\tempImage.jpg";
                       tempFilePath = tempFilePath.Replace("/", "\\");

                       previewPath = Server.MapPath("images/previews/" + manuId);
                       thumbNailPath = Server.MapPath("images/thumbnails/" + manuId);

                       if (!Directory.Exists(previewPath))
                       {
                           Directory.CreateDirectory(previewPath);
                       }
                       if (!Directory.Exists(thumbNailPath))
                       {
                           Directory.CreateDirectory(thumbNailPath);
                       }
                       fileNameOnly = "\\preview" + id+ ".jpg";
                       fileNameOnly = fileNameOnly.Replace("/", "\\");
                       previewPath = previewPath + fileNameOnly;
                       tempPartialPathP = "images\\previews\\" + manuId + fileNameOnly;

                       fileNameOnly = "\\thumbnail" + id+ ".jpg";
                       thumbNailPath = thumbNailPath + fileNameOnly;
                       tempPartialPathT = "images\\thumbnails\\" + manuId + fileNameOnly;


                       try
                       {

                           iProImg.GetFileFromUrl(path, tempFilePath);
                           rowCounter++;
                           iProImg.ReSizeImage(tempFilePath, previewPath, previewSize);
                           iProImg.ReSizeImage(previewPath, thumbNailPath, thumbNailSize);

                       }
                       catch (Exception ec)
                       {

                           errorRowCount++;
                           iLog.LogErrorToFile("uploadExcel", ec.ToString(), "path : " + path + ",tempFilePath :" + tempFilePath);


                       }
                       finally
                       {
                           if(File.Exists(tempFilePath))
                           {
                           File.Delete(tempFilePath);
                           }
                       }
                   } // If manuid!=""
               }  //if (dr[0].ToString() != "")

有人對如何解決此異常有任何建議嗎?

嘗試將您正在讀取/保存文件的 .Net 使用者的訪問權限設置為“完全控制”。

世界上沒有任何答案可以為我解決這個問題,直到我自己偶然發現了答案:

取消加密文件

您可以向整個硬碟驅動器上的每個人授予完全權限,但它仍然不允許 ASP.NET 解密文件。

如果您確定文件未加密,則只需將 ASPNET 帳戶添加到您要訪問的文件或文件夾中。但請確保它沒有先加密!

引用自:https://stackoverflow.com/questions/333571