2014-02-23 73 views
1

我想從服務器端下載PDF文件到客戶端系統。 「報告」頁面更改爲PDF文件並保存到服務器端的項目文件夾中。這裏的問題是,當我從客戶端系統訪問它並嘗試生成PDF文件時,我不確定它是否已成功生成PDF文件到服務器端項目文件夾中,而且它不會自動下載到客戶端系統中。但是當我從本地系統運行項目時,它正常工作。如何將PDF文件下載到客戶端系統?

我在這裏發佈我的代碼,請覈對,請給我一個解決方案,我需要非常

我的代碼是:

protected void btn_print_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       string url = HttpContext.Current.Request.Url.AbsoluteUri; 
       int width = 850; 
       int height = 550; 
       Thumbnail1 thumbnail = new Thumbnail1(url, 990, 1000, width, height); 
       Bitmap image = thumbnail.GenerateThumbnail(); 
       image.Save(Server.MapPath("~") + "/Dwnld/Thumbnail.bmp"); 
       imagepath = Server.MapPath("~").ToString() + "\\Dwnld\\" + "Thumbnail.bmp"; 
       imagepath1 = Server.MapPath("~").ToString() + "\\Dwnld\\" + "Thumbnail.pdf"; 
       convetToPdf(); 
      } 
      catch (Exception) 
      { 

       throw; 
      } 
     } 



    string imagepath = null; 
     string imagepath1 = null; 
     public void convetToPdf() 
     { 
      PdfDocument doc = new PdfDocument(); 
      System.Drawing.Size size = PageSizeConverter.ToSize(PdfSharp.PageSize.A4); 
      PdfPage pdfPage = new PdfPage(); 
      pdfPage.Orientation = PageOrientation.Landscape; 
      doc.Pages.Add(pdfPage); 
      // XSize size = PageSizeConverter.ToSize(PdfSharp.PageSize.A4) 
      XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]); 
      XImage img = XImage.FromFile(imagepath); 
      xgr.DrawImage(img, 0, 0); 
      doc.Save(imagepath1); 
      xgr.Dispose(); 
      img.Dispose(); 
      doc.Close(); 
      Response.ContentType = "Application/pdf"; 
      //Get the physical path to the file. 
      string FilePath = imagepath1; 
      //Write the file directly to the HTTP content output stream. 
      Response.WriteFile(FilePath); 
      Response.End(); 
     } 
     public class Thumbnail1 
     { 
      public string Url { get; set; } 
      public Bitmap ThumbnailImage { get; set; } 
      public int Width { get; set; } 
      public int Height { get; set; } 
      public int BrowserWidth { get; set; } 
      public int BrowserHeight { get; set; } 

      public Thumbnail1(string Url, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight) 
      { 
       this.Url = Url; 
       this.BrowserWidth = BrowserWidth; 
       this.BrowserHeight = BrowserHeight; 
       this.Height = ThumbnailHeight; 
       this.Width = ThumbnailWidth; 
      } 
      public Bitmap GenerateThumbnail() 
      { 
       Thread thread = new Thread(new ThreadStart(GenerateThumbnailInteral)); 
       thread.SetApartmentState(ApartmentState.STA); 
       thread.Start(); 
       thread.Join(); 
       return ThumbnailImage; 
      } 
      private void GenerateThumbnailInteral() 
      { 
       WebBrowser webBrowser = new WebBrowser(); 
       webBrowser.ScrollBarsEnabled = false; 
       webBrowser.Navigate(this.Url); 
       webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted); 
       while (webBrowser.ReadyState != WebBrowserReadyState.Complete) System.Windows.Forms.Application.DoEvents(); 
       webBrowser.Dispose(); 
      } 
      private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
      { 
       WebBrowser webBrowser = (WebBrowser)sender; 
       webBrowser.ClientSize = new Size(this.BrowserWidth, this.BrowserHeight); 
       webBrowser.ScrollBarsEnabled = false; 
       this.ThumbnailImage = new Bitmap(webBrowser.Bounds.Width, webBrowser.Bounds.Height); 
       webBrowser.BringToFront(); 
       webBrowser.DrawToBitmap(ThumbnailImage, webBrowser.Bounds); 
       this.ThumbnailImage = (Bitmap)ThumbnailImage.GetThumbnailImage(Width, Height, null, IntPtr.Zero); 
      } 
     } 
     protected void CreateThumbnailImage(object sender, EventArgs e) 
     { 

     } 
+0

請首先查看錯誤日誌。可能你沒有授予該文件的權限以便在磁盤上寫入。 – Aristos

回答

3

一個潛在的問題與此代碼是您正在爲每個請求寫入相同的文件。如果同時有多個請求,其中一些可能會失敗。

爲了解決這個問題,你可以寫信給直接響應流,即

protected void btn_print_Click(object sender, EventArgs e) 
{ 
    string url = HttpContext.Current.Request.Url.AbsoluteUri; 
    int width = 850; 
    int height = 550; 
    Thumbnail1 thumbnail = new Thumbnail1(url, 990, 1000, width, height); 
    using (Bitmap image = thumbnail.GenerateThumbnail()) 
     convertToPdf(image); 
} 

public void convertToPdf(Image image) 
{ 
    using (PdfDocument doc = new PdfDocument()) 
    { 
     System.Drawing.Size size = PageSizeConverter.ToSize(PdfSharp.PageSize.A4); 
     PdfPage pdfPage = new PdfPage(); 
     pdfPage.Orientation = PageOrientation.Landscape; 
     doc.Pages.Add(pdfPage); 
     using (XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0])) 
     { 
      using (XImage img = XImage.FromGdiPlusImage(image)) 
      { 
       xgr.DrawImage(img, 0, 0); 
       using (MemoryStream stream = new MemoryStream()) 
       { 
        doc.Save(stream, false); 
        Response.ContentType = "application/pdf"; 
        Response.AddHeader("Content-Length", stream.Length.ToString()); 
        stream.WriteTo(Response.OutputStream); 
       } 
      } 
     } 
    } 
    Response.End(); 
} 

編輯修改答案使用using語句來釋放資源。

+0

當我在上面的代碼嘗試顯示錯誤,即「指定的方法不支持」。在行「doc.Save(Response.OutputStream);」 –

+0

請給我解決方案,我如何可以將pdf文件從服務器保存到客戶端系統。當我從服務器計算機運行項目,然後它工作正常,並顯示PDF文件,並提供下載選項,但是當我從客戶端機器訪問它,那麼它不提供任何下載的選項,並不顯示PDF文件。 –

+0

使用臨時MemoryStream的答案中的修改代碼。可能Response.OutputStream不支持查找。請再試一次。 – alsed42