[英]IHttpHandler doesn't load image after deployed to web server
我現在有將網站部署到共享Windows托管的問題。
我目前與hostgator托管。
問題是,將項目部署到Web服務器后 ,應該返回圖像文件的ThumbnailHandler停止工作。 它可以在本地IIS虛擬文件夾和vs2010 debug中正常工作 。
例如-
http://www.myweb.com/imageHandler.ashx?i=0- 錯誤
http://www.myweb.com/imageHandler.ashx- 錯誤
http://www.myweb.com/image-handler?i=0- (使用IRouteHandler路由) 錯誤
沒有異常被拋出。 如果我打開網址,它將返回“由於包含錯誤,無法顯示圖像( 路徑 )”。
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="<,>,*,%,&,:,\,?" />
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
有誰之前經歷過這個嗎? 我也嘗試檢查文件夾權限安全設置。 任何幫助表示贊賞,在此先感謝。
注意:任何文件夾中的任何圖像都會發生這種情況。
當將圖像加載到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);
好的,我發現了問題。 從字面上看,我的托管服務提供商不支持完全信任策略,因此我不得不將其更改為中等信任。 即使我在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
但是,我仍然不知道為什么下面的代碼工作
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中在web.config中對此進行了測試
<trust level="Medium" originUrl=""/>
無論如何,現在就可以使用。 我仍然沒有合適的時間來找出為什么Stream類型對象的行為不同於Image.FromFile的原因,因為我知道的是文件的那兩個靜止流。 如果發現有趣的事情,我會發布更多更新。
干杯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.