Response.Flush()方法不起作用

时间:2013-09-17 11:43:53

标签: asp.net asp.net-mvc

Response.Flush方法无效。我在pdfContent上获取数据,但它没有打开PDF documnet。下面是代码。请帮忙。

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

2 个答案:

答案 0 :(得分:1)

而不是使用

return Content("")

使用它:

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

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

答案 1 :(得分: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();

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