HttpHandler不在服务器上呈现图像

时间:2017-06-21 11:03:37

标签: asp.net httphandler

我有一个FileHandler.ashx来实现IHttpHandler。这个处理程序唯一做的就是获取文件并通过context.Response返回它。

即使在发布之后(在我的本地机器中),它在我的本地单元中也能正常工作。虽然在服务器计算机上它只有在我通过Visual Studio运行网站时才有效,但如果我访问已发布的网站(即http://url.to.site/)则无效。

这是在我的本地计算机(通过Visual Studio或发布的站点)上运行时,以及通过服务器计算机上的Visual Studio运行时的样子: enter image description here

这就是访问服务器计算机上发布的网站时的样子:enter image description here

这是ProcessRequest的{​​{1}}方法的摘录:

FileHandler

var strExtenstion = Path.GetExtension(file); var fileInfo = new FileInfo(file); context.Response.Clear(); context.Response.ClearContent(); context.Response.AppendHeader("content-length", file.Length.ToString()); if (strExtenstion == ".pdf") { context.Response.ContentType = "application/pdf"; } else if (strExtenstion == ".png") { context.Response.ContentType = "image/png"; } else if (strExtenstion == ".jpg") { context.Response.ContentType = "image/jpeg"; } else if (strExtenstion == ".gif") { context.Response.ContentType = "image/gif"; } else if (strExtenstion == ".bmp") { context.Response.ContentType = "image/bmp"; } else if (strExtenstion == ".txt") { context.Response.ContentType = "text/plain"; } else { context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileInfo.Name); context.Response.ContentType = "application/octet-stream"; } context.Response.WriteFile(fileInfo.FullName); 是文件的路径。

** 更新 **

如果这会有所帮助:我使用的服务器是使用Plesk维护的GoDaddy服务器。

1 个答案:

答案 0 :(得分:1)

我找到了答案:我在这一行中使用了错误的变量:

context.Response.AppendHeader("content-length", file.Length.ToString());

file.Length.ToString()应该是fileInfo.Length.ToString(),但是这个代码在通过VS运行时不应该有效,很奇怪。