Skip to main content
 首页 » 编程设计

c#之使用 response.write 打开 byte[] pdf 文件(服务器端操作不起作用

2024年02月13日15wayfarer

我正在使用以下代码打开 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(); 

请您参考如下方法:

试试这个。它应该有效。

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); 

在上面的代码中。