【问题标题】:open byte[] pdf file using response.write( sever side actions are not working使用 response.write 打开 byte[] pdf 文件(服务器端操作不起作用
【发布时间】:2013-12-19 09:51:40
【问题描述】:

我正在使用以下代码打开 pdf byte[] 文件而不保存它。它工作正常,但在此操作之后,没有其他服务器端操作(如按钮单击)不起作用。回发不起作用。

    byte[] bytfile = Objects.GetFile(Convert.ToInt32(txtslno.Text.Trim()));
    Response.Clear();
    Response.ClearContent();
    Response.ClearHeaders();
    Response.ContentType = "application/pdf";
    Response.AddHeader("Content-Disposition", "attachment;filename="+filename);
    Response.AddHeader("Content-Length", bytfile.Length.ToString());
    Response.OutputStream.Write(bytfile, 0, bytfile.Length);
    Response.Flush();
    Response.End();

【问题讨论】:

    标签: c# asp.net response


    【解决方案1】:

    试试这个。它应该可以工作。

    byte[] bytfile = Objects.GetFile(Convert.ToInt32(txtslno.Text.Trim()));
    Response.Clear();
    MemoryStream ms = new MemoryStream(bytfile);
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=test.pdf");
    Response.Buffer = true;
    ms.WriteTo(Response.OutputStream);
    Response.End();
    

    再试试

    Response.BinaryWrite(bytfile);
    

    而不是

    ms.WriteTo(Response.OutputStream);
    

    在上面的代码中。

    【讨论】:

    • 还是同样的问题。你有什么其他想法可以解决这个问题。
    • @Nathiya 你的按钮在UpdatePanelTrigger中使用了吗?
    【解决方案2】:

    它工作正常,但在此操作之后没有其他服务器端操作 像按钮点击不起作用。

    您是在页面还是控件中使用您的代码?

    为您的目的使用通用处理程序 (*.ashx)。下载pdf的代码不会再给应用程序带来麻烦了。

    https://stackoverflow.com/a/12340735/225808 可能作为参考有用。

    【讨论】:

    • 我在页面中使用它
    • 尝试使用通用处理程序
    猜你喜欢
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-23
    • 2014-01-03
    • 1970-01-01
    • 2016-09-18
    • 1970-01-01
    相关资源
    最近更新 更多