【问题标题】:Cannot call handler ashx more than once when using Response.TransmitFile使用 Response.TransmitFile 时不能多次调用处理程序 ashx
【发布时间】:2013-03-15 12:10:50
【问题描述】:

我为客户从我的网站下载内容(视频)创建了一个 HttpHandler (.ashx)。首先我使用的是 WriteFile 方法,我意识到它需要大量内存,然后我决定将其更改为 TransmitFile 方法。

但是发生了一件奇怪的事情,我无法再进行多次下载。我必须等待下载完成并开始另一个。

基本上代码是这样的:

        System.IO.FileInfo file = new System.IO.FileInfo(file_path);

        context.Response.Clear();

        if (flagH264)
        {
            context.Response.ContentType = "video/mp4";
        }
        else
        {
            context.Response.ContentType = "video/x-ms-wmv";
        }

        context.Response.AddHeader("Content-Length", file.Length.ToString());
        context.Response.AddHeader("Content-Disposition", "attachment; filename=" + name);

        //context.Response.WriteFile(file_path.Trim());
        context.Response.TransmitFile(file_path.Trim());
        context.Response.Flush();

有人知道这是什么问题吗?

【问题讨论】:

    标签: file-io download httphandler ashx transmitfile


    【解决方案1】:

    我发现了问题所在。

    我用于下载页面的 HttpHandler (ashx) 正在实现一个接口 IRequireSessionState,它赋予我操作会话数据的读/写权限。当使用 TransmitFile 方法时,IIS 会阻止系统上的任何操作,以保护会话数据不被更改。

    解决方案是更改 IReadOnlySessionStateIRequireSessionState,它只提供对会话数据的读取访问权限,无需提供任何类型的安全性,阻止用户操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-16
      • 1970-01-01
      • 2011-03-07
      • 2016-02-02
      • 1970-01-01
      • 2011-09-19
      相关资源
      最近更新 更多