2012-02-13 45 views
3

我有一個在VS2010中完美運行的C#Web應用程序,但是當部署到IIS7服務器時,返回「圖像未找到圖標」。FileNotFoundException IIS7

的一段代碼在基本上問題抓住在網絡共享位置處的圖像的縮略圖,操縱和然後推回出到網頁。

Web服務器與其嘗試訪問的文件位於同一網絡上。所有訪問此網頁的用戶都位於同一個本地Intranet上。

的應用是在一個這樣或那樣的分類網絡資源節省的存儲列表。

當我部署應用程序,它提供了兩種錯誤,我在我的應用程序日誌中找到。我覺得這是一個文件權限錯誤,並且這兩個錯誤是鏈接的,但我不知道在哪裏更改權限以使應用程序正常工作。但是,如果我將「T:\ Published \ Generic.jpg」並將其插入到我的IE地址欄中,則可能會出現此問題。它加載圖像。

的用於處理圖像的一段代碼是這樣的:

System.Drawing.Image img; 
img = System.Drawing.Image.FromFile(MapPath(Request.QueryString["File"].ToString())); 

我已經既沒有MapPath方法試了一下。

我嘗試調試應用程序,但因爲它在VS2010中工作,所以它不會拋出異常,所以我不知道它爲什麼會在IIS服務器上拋出。

整個堆棧跟蹤的要求:imagedrawer.aspx的

Event code: 3005 
Event message: An unhandled exception has occurred. 
Event time: 13/02/2012 4:16:26 PM 
Event time (UTC): 13/02/2012 11:16:26 PM 
Event ID: 1f01693f71a2443790a8d83ba06a88a4 
Event sequence: 12 
Event occurrence: 1 
Event detail code: 0 

Application information: 
Application domain: /LM/W3SVC/2/ROOT-3-129736485835718008 
Trust level: Full 
Application Virtual Path:/
Application Path: C:\inetpub\wwwroot\ 
Machine name: XXXXXX 

Process information: 
Process ID: 10768 
Process name: w3wp.exe 
Account name: IIS APPPOOL\ASP.NET v4.0 

Exception information: 
Exception type: FileNotFoundException 
Exception message: T:\Published\Generic.jpg 
at System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement) 
at imagedrawer.Page_Load(Object sender, EventArgs e) 
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) 
at System.Web.UI.Control.LoadRecursive() 
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 



Request information: 
Request URL: http://localhost/imagedrawer.aspx?File=T:\Published\Generic.jpg 
Request path: /imagedrawer.aspx 
User host address: ::1 
User: 
Is authenticated: False 
Authentication Type: 
Thread account name: IIS APPPOOL\ASP.NET v4.0 

Thread information: 
Thread ID: 64 
Thread account name: IIS APPPOOL\ASP.NET v4.0 
Is impersonating: False 
Stack trace: at System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement) 
at imagedrawer.Page_Load(Object sender, EventArgs e) 
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) 
at System.Web.UI.Control.LoadRecursive() 
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 


Custom event details: 

內容:

System.IO.MemoryStream ms = new System.IO.MemoryStream(); 
     System.Drawing.Image img; 

     img = System.Drawing.Image.FromFile(MapPath(Request.QueryString["File"].ToString())); 


     if (img.Height > 80 || img.Width > 80) 
     { 
      System.Drawing.RectangleF RF = new System.Drawing.RectangleF(); 
      RF.X = 0; 
      RF.Y = 0; 
      RF.Height = (img.Height < 80) ? img.Height : 80; 
      RF.Width = (img.Width < 80) ? img.Width : 80; 
      System.Drawing.Bitmap bmthumb = (System.Drawing.Bitmap)img.Clone(); 
      System.Drawing.Bitmap bmCrop = bmthumb.Clone(RF, bmthumb.PixelFormat); 
      img = (System.Drawing.Image)bmCrop; 
     } 
     img.Save(ms, System.Drawing.Imaging.ImageFormat.Png); 


     Response.Clear(); 
     Response.AddHeader("Content-Disposition", "attachment; filename=" + Request.QueryString["File"].ToString()); 
     Response.AddHeader("Content-Length", ms.ToArray().Length.ToString()); 
     Response.ContentType = "image/jpeg"; 
     Response.BinaryWrite(ms.ToArray()); 
     Response.End(); 
     img.Dispose(); 

回答

3

我不認爲網絡驅動器在服務環境下可用。您可能必須使用網絡共享符號(例如\\machine-name\share)。此外,您正在默認的用戶上下文(IIS APPPOOL\ASP.NET v4.0)下運行,這在網絡設置中更難實現。您應該將應用程序池標識更改爲網絡用戶,並授予該用戶訪問權限。

另一種方法是模擬用戶訪問應用程序(假設你使用Windows身份驗證)。

您可以通過在應用程序池右擊並選擇高級設置更改應用程序池標識。過程模型下的標識是要更改的設置。

爲了啓用模擬,您可以轉到應用程序,然後選擇身份驗證功能,啓用ASP.NET模擬,然後單擊編輯..並確保已選擇身份驗證的用戶。通過在最後一個對話框中使用特定用戶,模擬也可以使用特定的用戶身份,但是當您想要在通常無法作爲服務運行的用戶的上下文中運行時,此模式非常有用。

編輯:

顯然,IIS程序池用戶機上下文,這是DOMAIN\Machine$下運行。請參閱Application Pool Identities

+1

@JKM,當您使用IE訪問共享時,IE使用您的憑據。如果IE瀏覽器有效,它只是證明你有權訪問該共享。但是,在IIS/ASP.NET上運行的應用程序正在應用程序池標識下運行(如Guvante所述),因此發生的任何錯誤都表明該標識不能訪問該共享(必須使用UNC路徑,每http:// support.microsoft.com/kb/257174)。您需要授予訪問共享池身份的權限,或將訪問池身份更改爲可訪問共享的帳戶。下次請多關注用戶上下文。 – 2012-02-14 02:49:04

+0

將解決方案部署到目標機器並設置正確的應用程序池服務後,它不再註冊此錯誤。 – Sorean 2012-02-14 18:32:22

2

的IIS7工作進程在其自己的憑據運行。它將以運行網站所運行的應用程序池的身份訪問該文件。這通常是ApplicationPoolIdentityNetworkService。您需要授予該用戶對該文件的訪問權限。

但是,如果你真的得到FileNotFoundException這可能不是你的問題,所以請發佈整個堆棧跟蹤。

+0

主要發佈更新請求的信息。 – Sorean 2012-02-13 23:34:21

+0

@JKM我最好的猜測是驅動器字母「T」未在服務器上正確映射。 – 2012-02-13 23:35:32

+0

@JKM我建議使用UNC路徑,比如'\\ servername \ folder \ file.name'而不是映射驅動器。 – 2012-02-13 23:36:57

0

我認爲這是因爲您正在使用映射的驅動器名稱訪問映像。 相反,如果在IIS虛擬目錄中使用T:\ Published \ Generic.jpg嘗試UNC名稱\ machineName \ Published \ Generic.jpg

+0

只需輸入URL即可瀏覽圖片的形象?沒有通過你的imagedrawer.aspx? – ASetty 2012-02-13 23:45:14