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

Gee*_*eth 1 c# asp.net response

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

San*_*etS 5

尝试这个。它应该工作。

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();
Run Code Online (Sandbox Code Playgroud)

否则试试

Response.BinaryWrite(bytfile);
Run Code Online (Sandbox Code Playgroud)

代替

ms.WriteTo(Response.OutputStream);
Run Code Online (Sandbox Code Playgroud)

在上面的代码中。