C# IHttpHandler不';部署到web服务器后无法加载映像
我现在无法将我的网站部署到共享windows主机 我目前正在用hostgator托管它 问题是,我的ThumbnailHandler应该返回一个图像文件,但一旦项目部署到web服务器上,它就停止工作了。它在本地IIS虚拟文件夹和vs2010调试中正常工作 e、 g- -错误 -错误 -(使用IRoutHandler路由) 错误 -好的强> 没有抛出异常。如果我打开url,它会返回“图像(路径)无法显示,因为它包含错误” ThumbnailHandler.ashxC# IHttpHandler不';部署到web服务器后无法加载映像,c#,asp.net,shared-hosting,C#,Asp.net,Shared Hosting,我现在无法将我的网站部署到共享windows主机 我目前正在用hostgator托管它 问题是,我的ThumbnailHandler应该返回一个图像文件,但一旦项目部署到web服务器上,它就停止工作了。它在本地IIS虚拟文件夹和vs2010调试中正常工作 e、 g- -错误 -错误 -(使用IRoutHandler路由) 错误 -好的 没有抛出异常。如果我打开url,它会返回“图像(路径)无法显示,因为它包含错误” ThumbnailHandler.ashx public void Proces
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; } }
public void ProcessRequest(HttpContext上下文){
context.Response.Flush();
尺寸maxSize=新尺寸(98,98);
byte[]buffer=Imaging.GetImage(context.Server.MapPath(“~/img/noimage98.png”),maxSize).GetBytes(System.Drawing.Imaging.ImageFormat.Jpeg);
试一试{
Int32 index=GetImageIndex(上下文);
maxSize=GetMaxSize(上下文);
如果(索引<0 | | maxSize==null)
抛出新异常(“未指定索引大小”);
String authToken=GetAuthenticationToken(上下文);
DirectoryInfo-DirectoryInfo=AdvertisementDirectory.GetDirectoryInfo(authToken);
List fileInfos=AdvertisementDirectory.GetImageFileInfoList(directoryInfo);
使用(System.Drawing.Image thumbnailImage=Imaging.GetImage(fileInfos[index].FullName,maxSize)){
buffer=thumbnailImage.GetBytes(System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
捕获(例外情况除外){
掷骰子;
}
最后{
context.Response.ContentType=“image/jpeg”;
context.Response.OutputStream.Write(buffer,0,buffer.Length);
context.Response.Flush();
context.Response.Close();
context.ApplicationInstance.CompleteRequest();
}
}
公共布尔可重用{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>
好的,我发现了问题所在。
实际上,我的主机提供商不支持完全信任策略,所以我不得不将其更改为“中等信任”。即使我在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 (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中测试了它
不管怎样,它现在起作用了。
我仍然没有找到合适的时间来找出为什么流类型对象的行为与Image.FromFile不同,因为我知道的是该文件的两个静态流。
如果我发现了一些有趣的事情,我会发布更多的更新
干杯。正如我所说,我已经三次检查了相应的文件夹权限,并允许每个用户访问它。对于完整路径映像,因为它是共享的windows主机。我想我无法访问根路径。但是,我还没有尝试从“url”获取图像,我会尝试并更新您。谢谢:)嘿,从Uri使用流实际上是有效的。谢谢:)。但由于某些原因,我仍然需要使用MapPath方法。对我还有其他建议吗?因此,如果我使用
FromFile
,而不是使用FromStream
,这个信任问题就会得到解决。
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);
}
<trust level="Medium" originUrl=""/>