HttpHandler 不在服务器上呈现图像
HttpHandler doesn't render image on server
我有一个 FileHandler.ashx
实现了 IHttpHandler
。这个处理程序唯一做的就是获取文件并 return 它通过 context.Response
.
即使在发布后(在我的本地机器上),它在我的本地单元中也能正常工作。在服务器机器上,它仅在我通过 Visual Studio 运行 网站时有效,如果我访问已发布的网站(即 http://url.to.site/),则它不起作用。
这是在我的本地计算机上 运行ning(通过 Visual Studio 或已发布的站点)以及在服务器计算机上 Visual Studio 时的样子:
这是在服务器计算机上访问已发布网站时的样子:
这是 FileHandler
的 ProcessRequest
方法的片段:
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);
file
为文件路径
** 更新 **
如果这会有所帮助:我使用的服务器是使用 Plesk 维护的 GoDaddy 服务器。
我找到了答案:我在这一行中使用了错误的变量:
context.Response.AppendHeader("content-length", file.Length.ToString());
file.Length.ToString()
应该是 fileInfo.Length.ToString()
,但是当 运行 通过 VS 时这段代码不应该起作用,很奇怪。
我有一个 FileHandler.ashx
实现了 IHttpHandler
。这个处理程序唯一做的就是获取文件并 return 它通过 context.Response
.
即使在发布后(在我的本地机器上),它在我的本地单元中也能正常工作。在服务器机器上,它仅在我通过 Visual Studio 运行 网站时有效,如果我访问已发布的网站(即 http://url.to.site/),则它不起作用。
这是在我的本地计算机上 运行ning(通过 Visual Studio 或已发布的站点)以及在服务器计算机上 Visual Studio 时的样子:
这是在服务器计算机上访问已发布网站时的样子:
这是 FileHandler
的 ProcessRequest
方法的片段:
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);
file
为文件路径
** 更新 **
如果这会有所帮助:我使用的服务器是使用 Plesk 维护的 GoDaddy 服务器。
我找到了答案:我在这一行中使用了错误的变量:
context.Response.AppendHeader("content-length", file.Length.ToString());
file.Length.ToString()
应该是 fileInfo.Length.ToString()
,但是当 运行 通过 VS 时这段代码不应该起作用,很奇怪。