3
我有一個下載處理程序,它將在IE下載文件,但在chrome試圖下載自己。我的意思是Chrome會下載一個名爲downloadhandler.ashx 該處理程序的代碼是ASP.NET下載處理程序在IE瀏覽器但不是Chrome
<%@ WebHandler Language="C#" Class="DownloadHandler" %>
using System;
using System.Web;
public class DownloadHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string file = "";
// get the file name from the querystring
if (context.Request.QueryString["Filepath"] != null)
{
file = context.Request.QueryString["Filepath"].ToString();
}
string filename = context.Server.MapPath("~/Minutes/" + file);
System.IO.FileInfo fileInfo = new System.IO.FileInfo(filename);
try
{
if (fileInfo.Exists)
{
context.Response.Clear();
context.Response.AddHeader("Content-Disposition", "inline;attachment; filename=\"" + fileInfo.Name + "\"");
context.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
context.Response.ContentType = "application/octet-stream";
context.Response.TransmitFile(fileInfo.FullName);
context.Response.Flush();
}
else
{
throw new Exception("File not found");
}
}
catch (Exception ex)
{
context.Response.ContentType = "text/plain";
context.Response.Write(ex.Message);
}
finally
{
context.Response.End();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
處理程序從這段代碼
private static string BuildAbsolute(string relativeUri)
{
// get current uri
Uri uri = HttpContext.Current.Request.Url;
// build absolute path
string app = HttpContext.Current.Request.ApplicationPath;
if (!app.EndsWith("/")) app += "/";
relativeUri = relativeUri.TrimStart('/');
// return the absolute path
return HttpUtility.UrlPathEncode(
String.Format("http://{0}:{1}{2}{3}",
uri.Host, uri.Port, app, relativeUri));
}
這是在此使用接收其信息方法
public static string ToDownloadMinutes(string fileName)
{
return BuildAbsolute(String.Format("Handlers/DownloadHandler.ashx?Filepath={0}", fileName));
}
獲得此下載功能在Chrome中的任何幫助將不勝感激。
我會改變 context.Response.ContentType =「應用/八位字節流「; by context.Response.ContentType = Net.Mime.MediaTypeNames.Application.Octet 養成使用這種習慣,而且不會因爲錯別字而導致時間不足。 – JDC 2012-04-18 08:09:43