先生,如何将下载的pdf文件的副本保存到虚拟文件夹(表示我的Web应用程序中的文件夹) [英] sir,how to save copy of the downloaded pdf file to virtual folder(means folder in my web application)

查看:64
本文介绍了先生,如何将下载的pdf文件的副本保存到虚拟文件夹(表示我的Web应用程序中的文件夹)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void Button1_Click(object sender, EventArgs e)
        {
            GeneratePDF();
        }
        private void GeneratePDF()
        {
            DataTable dtTemp = new DataTable();
            dtTemp.Columns.Add("Customer ID");
            dtTemp.Columns.Add("First Name");
            dtTemp.Columns.Add("Last Name");
            dtTemp.Columns.Add("Address");
            dtTemp.Rows.Add("0001", "Peter", "p", "2376, 00800, calicut");
            dtTemp.Rows.Add("0002", "Alfred", "k", "3453, 00800, calicut");
            dtTemp.Rows.Add("0003", "Alice", "n", "6056, 00800, calicut");
            dtTemp.Rows.Add("0004", "Denis", "h", "5672, 00800, calicut");
            dtTemp.Rows.Add("0005", "Paul", "k", "8734, 00800, calicut");
            Document pdfDoc = new Document(PageSize.A4, 30, 30, 40, 25);
            System.IO.MemoryStream mStream = new System.IO.MemoryStream();
            PdfWriter writer = PdfWriter.GetInstance(pdfDoc, mStream);
            int cols = dtTemp.Columns.Count;
            int rows = dtTemp.Rows.Count;
            pdfDoc.Open();

            iTextSharp.text.Table pdfTable = new iTextSharp.text.Table(cols, rows);
            pdfTable.BorderWidth = 1;
          
            pdfTable.Padding = 1;
            pdfTable.Spacing = 1;

            //creating table headers
            for (int i = 0; i < cols; i++)
            {
                Cell cellCols = new Cell();
                Font ColFont = FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.BOLD);
                Chunk chunkCols = new Chunk(dtTemp.Columns[i].ColumnName, ColFont);
                cellCols.Add(chunkCols);
                pdfTable.AddCell(cellCols);

            }
            //creating table data (actual result)
            for (int k = 0; k < rows; k++)
            {
                for (int j = 0; j < cols; j++)
                {
                    Cell cellRows = new Cell();
                    Font RowFont = FontFactory.GetFont(FontFactory.HELVETICA, 12);
                    Chunk chunkRows = new Chunk(dtTemp.Rows[k][j].ToString(), RowFont);
                    cellRows.Add(chunkRows);
                    pdfTable.AddCell(cellRows);

                }
            }

            pdfDoc.Add(pdfTable);
            pdfDoc.Close();
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "attachment; filename=Report.pdf");
            Response.Clear();
            Response.BinaryWrite(mStream.ToArray());

            Response.End();
     

        }

推荐答案

如果这是一个Web应用程序,这意味着实际上是Response HttpResponse对象。在这种情况下你可以使用



If this is a web application, this means that Response is actually HttpResponse. In that case you can use

string filename = "wherever you want";
Response.WriteFile(filename);


如果你需要这个:

下载后我需要这个pdf文件的副本到我的web应用程序中的文件夹(不是系统文件夹)



然后你不应该使用HttpResponse而是使用普通文件IO来保存文件。使用Server.MapPath查找Web服务器上的文件位置。为了安全起见,您可以将文件保存在单独的文件夹中,并限制所有其他文件夹上的文件访问权限。
If you need this:
"after download i need the copy of this pdf file to folder in my web application(not system folder)"

then you should not use HttpResponse but plain File IO to save the file. Use "Server.MapPath" to find the file location on your webserver. For safety you can save the file in a seperate folder and restrict file access on all other folders.


这篇关于先生,如何将下载的pdf文件的副本保存到虚拟文件夹(表示我的Web应用程序中的文件夹)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆