【问题标题】:Pdf file damaged on download in Chrome and Firefox在 Chrome 和 Firefox 中下载时损坏的 PDF 文件
【发布时间】:2013-01-31 20:25:35
【问题描述】:

我正在使用 iTextSharp 创建 pdf 报告(文件)并将其存储在我的应用程序所在的 Web 服务器上。我能够创建文件,进入存储文件夹并毫无问题地打开文件。注意:用户不会自动获取文件 创建时下载。

我想为用户提供一个选项,通过一个按钮从服务器下载“旧”报告。 这在 IE (10) 中运行良好,但在 Chrome 和 Firefox 中运行良好。我总是收到错误消息: 打开此文档时出错。文件已损坏,无法修复。

我有这个图像按钮,点击时我会根据this post 将用户发送到通用处理程序(因为我的页面包含更新面板)(目前仅部分使用它)。

这是实际下载文件的代码:

 public void ProcessRequest(HttpContext context)
    {
        var _fileName = context.Request.QueryString["fileName"];

        using (var _output = new MemoryStream())
        {
            //var _fileSeverPath = context.Server.MapPath(_fileName);
            context.Response.Clear();
            context.Response.ContentType = "application/pdf";// "application/pdf";
            //context.Response.AppendHeader("Content-Length", _fileName.Length.ToString());
            context.Response.AppendHeader("Content-Disposition", string.Format("attachment; filename=" + Path.GetFileName(_fileName)));

            context.Response.WriteFile(_fileName);
            context.Response.Flush();
            context.Response.Close();
            context.Response.End();
        }
    }

正如我所说,这在 IE 中可以正常工作,但在 Chrome 和 Firefox 中则不行。 当我在记事本中打开文件时,我发现在 Chrome 和 Firefox 中下载时,我只得到了大约 1/3 的文件。

任何建议将不胜感激。这几天一直在尝试解决这个问题..

【问题讨论】:

  • _output 有什么用处?
  • 尝试发送一个人造的非常大 (128MB) 的文件。发生什么了?还是损坏了?通过了多少。
  • 如果存储在服务器上的文件在服务器上打开时是正确的 PDF 文件,则 iTextSharp 创建了正确的 PDF,问题是由其他原因引起的。我建议从问题中删除 itextsharp 标签。
  • 对不起,当我尝试 Response.WriteBinary(_output.ToArray()) 时使用了输出......也没有工作。同意,也许 iTextSharp 标签不应该在那里

标签: asp.net .net c#-4.0 browser download


【解决方案1】:

来自HttpResponse.WriteFile Method (String)

当此方法用于大文件时,调用该方法可能 抛出异常。可以与此一起使用的文件的大小 方法取决于 Web 服务器的硬件配置。为了 更多信息,请参阅文章 812406,“PRB:Response.WriteFile 不能 在 Microsoft 知识库网站上下载大文件。

试试这个:

public void ProcessRequest(HttpContext context)
{
    var _fileName = context.Request.QueryString["fileName"];
    context.Response.Clear();
    context.Response.Buffer = true;
    context.Response.ContentType = "application/pdf";
    context.Response.AppendHeader(
        "Content-Disposition",
        string.Format("attachment; filename=" + Path.GetFileName(_fileName)));

    using (var fs = new FileStream(_fileName, FileMode.Open, FileAccess.Read))
    {
        using (var sr = new StreamReader(fs, true))
        {
            int length = (int)fs.Length;
            byte[] buffer;

            using (BinaryReader br = new BinaryReader(fs, sr.CurrentEncoding))
            {
                buffer = br.ReadBytes(length);
                context.Response.BinaryWrite(buffer);
            }
        }
    }
    context.Response.Flush();
    context.Response.Close();
    context.Response.End();
}

【讨论】:

  • 将读取数组的末尾。实际上,我不相信 WriteFile 问题是真实的。它会使此功能无用。这是一个重要的功能。如果它不抛出,我希望它能够工作。
  • 文件一点也不大。 44kb
  • 这是我按照您的建议更改代码后遇到的错误
【解决方案2】:

好的,终于..我找到了解决方案,同时它让我觉得自己像个傻瓜.. 删除 context.Response.Close(); ...然后一切都很完美:)

【讨论】:

    猜你喜欢
    • 2019-08-19
    • 1970-01-01
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-14
    • 2012-03-31
    • 1970-01-01
    相关资源
    最近更新 更多