0

Response.Flush 方法不起作用。我在 pdfContent 获取数据,但它没有打开 PDF 文档。下面是代码。请帮忙。

public ActionResult PdfClick(int requestid)
    {
        BusinessRequestController bsnrqstcntrlr = new BusinessRequestController();
        try
        {
             int DocId = (new BusinessRequestBR()).GetBaseLineDocumentsForSearch(requestid);
             byte[] PdfContent = (new BusinessRequestHelper()).GetBaseLineDonload(DocId);
             Response.Buffer = true;
             Response.Clear();
             Response.ContentType = "application/pdf";
             Response.AddHeader("content-disposition", "attachment; filename=" + "BaseLine_Doc" + "_" + DocId + ".pdf");
             Response.BinaryWrite(PdfContent);
             Response.Flush();
             return Content("");

        }
        catch (Exception ex)
        {
            throw this.log.CreatePropagatedException(ex);
        }
4

2 回答 2

1

而不是使用

return Content("")

用这个:

return File(
    PdfContent, 
    "application/pdf", 
    string.Format("BaseLine_Doc{0}.pdf", DocId)
)

无需操纵响应。只需返回正确的结果类型。

于 2013-09-17T11:47:26.997 回答
0

从我正在运行的项目中提取代码

Response.Clear();
Response.AddHeader("Content-Disposition","attachment; filename=\"" + file[0].Name + "\"");
Response.AddHeader("Content-Length", fileSize.ToString(_culture));                                         
Response.ContentType = Path.GetExtension(file[0].Name).GetMimeType();
Response.BufferOutput = false;
Response.AddHeader("Accept-Ranges", "bytes"); //tell the client that we accept byte-range requests

      while (totalBytesRead < fileSize)
         {
            if (!Response.IsClientConnected)
               {
                  System.Diagnostics.Debug.WriteLine("--> Client disconnected");
                         break;
                 }

                int bytesRead = fs.Read(buffer, 0, buffer.Length);
                Response.OutputStream.Write(buffer, 0, bytesRead);
                Response.Flush();
                totalBytesRead += bytesRead;
                                    }
   Response.End();

此代码每次都适用于我,请根据您的需要进行操作。注意:变量在代码上方定义。

于 2013-09-17T11:59:56.230 回答