2013-06-13 37 views
0

我現在有將我的網站部署到共享Windows主機的問題。IHttpHandler在部署到Web服務器後不加載圖像

我目前正在託管它與hostgator。

問題是,我的ThumbnailHandler應該返回一個圖像文件,一旦項目被部署到網絡服務器,就停止工作。它在本地IIS虛擬文件夾和vs2010調試中正常工作。

e.g-

http://www.myweb.com/imageHandler.ashx?i=0 - ERROR

http://www.myweb.com/imageHandler.ashx - ERROR

http://www.myweb.com/image-handler?i=0 - (路由與IRouteHandler) ERROR

http://www.myweb.com/img/theimage.jpg - OK!

不引發任何異常。如果我打開網址,則返回'圖像(路徑)無法顯示,因爲它包含錯誤'。

ThumbnailHandler.ashx

public void ProcessRequest(HttpContext context) { 
    context.Response.Flush(); 
    Size maxSize = new Size(98, 98); 
    byte[] buffer = Imaging.GetImage(context.Server.MapPath("~/img/noimage98.png"), maxSize).GetBytes(System.Drawing.Imaging.ImageFormat.Jpeg); 
    try { 
     Int32 index = GetImageIndex(context); 
     maxSize = GetMaxSize(context); 
     if (index < 0 || maxSize == null) 
      throw new Exception("Index size is not specified."); 
     String authToken = GetAuthenticationToken(context); 
     DirectoryInfo directoryInfo = AdvertisementDirectory.GetDirectoryInfo(authToken); 
     List<FileInfo> fileInfos = AdvertisementDirectory.GetImageFileInfoList(directoryInfo); 
     using (System.Drawing.Image thumbnailImage = Imaging.GetImage(fileInfos[index].FullName, maxSize)) { 
      buffer = thumbnailImage.GetBytes(System.Drawing.Imaging.ImageFormat.Jpeg); 
     } 
    } 
    catch (Exception ex) { 
     throw ex; 
    } 
    finally { 
     context.Response.ContentType = "image/jpeg"; 
     context.Response.OutputStream.Write(buffer, 0, buffer.Length); 
     context.Response.Flush(); 
     context.Response.Close(); 
     context.ApplicationInstance.CompleteRequest(); 
    } 
} 
public bool IsReusable { get { return false; } } 

的web.config

<globalization requestEncoding="iso-8859-1" responseEncoding="iso-8859-1" fileEncoding="utf-8" culture="en-US" uiCulture="en-US"/> 

<pages buffer="true" clientIDMode="Static" controlRenderingCompatibilityVersion="4.0" enableViewStateMac="true" validateRequest="true" enableEventValidation="true" enableSessionState="true" viewStateEncryptionMode="Auto" > 
    <controls> 
    <add tagPrefix="ATK" assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" /> 
    </controls> 
</pages> 

<httpHandlers> 
    <add verb="*" path="*.ashx" type="Tradepost.ThumbnailHandler, Tradepost"/> 
</httpHandlers> 

<httpRuntime executionTimeout="600" maxRequestLength="40000" maxUrlLength="260" maxQueryStringLength="2048" requestLengthDiskThreshold="80" useFullyQualifiedRedirectUrl="false" minFreeThreads="8" minLocalRequestFreeThreads="4" appRequestQueueLimit="5000" enableKernelOutputCache="true" enableVersionHeader="true" requireRootedSaveAsPath="true" enable="true" shutdownTimeout="90" delayNotificationTimeout="5" waitChangeNotification="0" maxWaitChangeNotification="0" enableHeaderChecking="true" sendCacheControlHeader="true" apartmentThreading="false" requestPathInvalidCharacters="&lt;,&gt;,*,%,&amp;,:,\,?" /> 

<system.webServer> 
<validation validateIntegratedModeConfiguration="false"/> 
<modules runAllManagedModulesForAllRequests="true" /> 
</system.webServer> 

有沒有人經歷過這個?我也試着檢查文件夾權限安全設置。任何幫助表示讚賞,在此先感謝。

注意:這發生在任何文件夾中的任何圖像。

回答

0

好吧,我發現了問題。 從字面上看,我的主機提供商不支持完全信任策略,所以我不得不將其更改爲中等信任。即使我對web.config完全信任,它也會將其更改回中等信任或顯示錯誤。

所以,在我的 '的getImage' 功能,這是

public static System.Drawing.Image GetImage(String fileName, System.Drawing.Size maxSize) { 
    System.Drawing.Image result = null; 
    try { 
     using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { 
      using (System.Drawing.Image img = System.Drawing.Image.FromStream(fs, true, false)) { 
       Size sz = GetProportionalSize(maxSize, img.Size); 
       result = img.GetThumbnailImage(sz.Width, sz.Height, null, IntPtr.Zero); 
       } 
      fs.Close(); 
     } 
    } 
    catch (Exception ex) { 
     throw ex; 
    } 
    return result; 
} 

using (FileStream fs = File.OpenRead(fileName)) { 
    using (StreamReader sr = new StreamReader(fs)) { 
     using (System.Drawing.Image img = System.Drawing.Image.FromStream(sr.BaseStream, true, false)) { 
      System.Drawing.Size sz = GetProportionalSize(maxSize, img.Size); 
      result = img.GetThumbnailImage(sz.Width, sz.Height, null, IntPtr.Zero); 
     } 
     sr.Close(); 
    } 
    fs.Close(); 
} 

並引發...發生錯誤

System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. at System.Security.CodeAccessSecurityEngine.CheckNReturnSO(PermissionToken permToken, CodeAccessPermission demand, StackCrawlMark& stackMark, Int32 unrestrictedOverride, Int32 create) at System.Security.CodeAccessSecurityEngine.Assert(CodeAccessPermission cap, StackCrawlMark& stackMark) at System.Security.CodeAccessPermission.Assert() at [Snipped Method Name] at ReportExprHostImpl.CustomCodeProxy.[Snipped Method Name] The action that failed was: Demand The type of the first permission that failed was: System.Security.Permissions.SecurityPermission The Zone of the assembly that failed was: MyComputer 

,但 我仍然不知道爲什麼下面的代碼WORKS

using (System.Drawing.Image img = System.Drawing.Image.FromFile(fileName)) { 
    System.Drawing.Size sz = GetProportionalSize(maxSize, img.Size); 
    result = img.GetThumbnailImage(sz.Width, sz.Height, null, IntPtr.Zero); 
} 

我已經在我的網頁與我的主機提供商和我的本地IIS這個測試它的.config

<trust level="Medium" originUrl=""/> 

不管怎麼說,現在的工作。 我還沒有找到合適的時間來找出爲什麼Stream類型的對象對Image.FromFile的行爲不同,因爲我知道的是這兩個仍然是該文件的流。 如果我發現了一些有趣的東西,我會發布更多更新。

乾杯。

+0

因此,如果我使用'FromFile',則不會使用'FromStream',而是解決此信任問題。 – choz

1

當圖像以IMG文件夾負載,IMG文件夾中有沒有訪問讀/寫文件。 所以首先給所有訪問img文件夾來讀寫圖像。

,或者你嘗試,因爲一段時間的客戶端服務器有不同的根路徑所以Server.MapPath

沒有得到圖像的準確路徑給圖像的完整路徑。

您可以嘗試在web.config appsetting控件中啓動域名定義路徑,然後在處理程序中使用此路徑獲取圖像路徑。

<appSettings> 
     <add key="ProjectName" value="/www.myweb.com"/> 
</appSettings> 

在處理程序頁面利用

string ProjectName = System.Configuration.ConfigurationManager.AppSettings["ProjectName"].ToString().Trim(); 
byte[] buffer = Imaging.GetImage(ProjectName+"/img/noimage98.png", maxSize).GetBytes(System.Drawing.Imaging.ImageFormat.Jpeg); 
+0

正如我所說,我已經三重檢查了相應的文件夾權限,並允許每個用戶訪問它。 在全路徑圖像的情況下,因爲它是共享Windows主機。我不認爲我能夠訪問根路徑。 但是,我還沒有試圖從'網址'獲取圖片,我會嘗試並更新你。謝謝:) – choz

+0

嘿,使用來自Uri的流實際上工作。謝謝 :)。 但由於某種原因,我仍然需要使用MapPath方法。對我有其他建議嗎? – choz