Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET下载处理程序适用于IE,但不适用于Chrome_C#_Asp.net_Ashx - Fatal编程技术网

C# ASP.NET下载处理程序适用于IE,但不适用于Chrome

C# ASP.NET下载处理程序适用于IE,但不适用于Chrome,c#,asp.net,ashx,C#,Asp.net,Ashx,我有一个下载处理程序,在IE中下载文件,但在chrome中它会尝试自行下载。我的意思是Chrome试图下载一个名为downloadhandler.ashx的文件 处理程序的代码是 <%@ WebHandler Language="C#" Class="DownloadHandler" %> using System; using System.Web; public class DownloadHandler : IHttpHandler { public void Proces

我有一个下载处理程序,在IE中下载文件,但在chrome中它会尝试自行下载。我的意思是Chrome试图下载一个名为downloadhandler.ashx的文件 处理程序的代码是

<%@ 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;
    }
}



}
在这个方法中使用的是什么

public static string ToDownloadMinutes(string fileName)
{
    return BuildAbsolute(String.Format("Handlers/DownloadHandler.ashx?Filepath={0}", fileName));
}
如果您能在Chrome中使用此下载功能,我们将不胜感激。

请尝试从内容处置标题中删除“内联”,因为“内联”和“附件”不应一起使用:

context.Response.AddHeader("Content-Disposition", "attachment; filename=\""
    + fileInfo.Name + "\"");

我将更改context.Response.ContentType=“应用程序/八位字节流”;通过context.Response.ContentType=Net.Mime.MediaTypeNames.Application.Octet养成使用此选项的习惯,您就不会因为输入错误而浪费时间。
context.Response.AddHeader("Content-Disposition", "attachment; filename=\""
    + fileInfo.Name + "\"");