如何将PDF文件下载到客户端系统?

时间:2014-02-23 10:16:41

标签: c# asp.net pdfsharp

我想从服务器端下载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)
        {

        }

1 个答案:

答案 0 :(得分: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语句释放资源。