【问题标题】:Common file directory to save PDF in both computer and phone用于在电脑和手机中保存 PDF 的通用文件目录
【发布时间】:2013-07-09 04:30:53
【问题描述】:

目前我完成了我的 PDF webapp 的开发,我可以在其中生成各种 PDF。但是,当用户访问时,我需要为我的手机和计算机提供一个通用的下载PDF目录。

就目前而言,当我尝试在 localhost 上运行我的 webapp 时,这就是我保存 PDF 文件的方式。但是,当我在线发布我的 web 应用程序时,并非所有计算机都有类似的文件目录/

var output = new FileStream(Path.Combine("C:\\Users\\apr13mpsip\\Downloads", filename), FileMode.Create);

所以想请教各位大神有没有关于手机和电脑共用一个文件目录的想法。

PDF按钮

protected void btnPDF_Click(object sender, EventArgs e)
    {

        var doc1 = new Document();
        var filename = "MyTestPDF" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".pdf";
        var output = new FileStream(Path.Combine("C:\\Users\\apr13mpsip\\Downloads", filename), FileMode.Create);
        iTextSharp.text.pdf.PdfWriter.GetInstance(doc1, output);
        doc1.Open();

        PdfPTable table = new PdfPTable(1);
        table.TotalWidth = 585f;
        table.LockedWidth = true;

        var logo = iTextSharp.text.Image.GetInstance(Server.MapPath("~/image/logo.jpg"));
        doc1.Add(logo);

        var titleFont = FontFactory.GetFont("Arial", 18, Font.BOLD);
        doc1.Add(new Paragraph("Officer's Profile. Officer's Police ID: " + DDLCase.SelectedValue, titleFont));

        var normalFont = FontFactory.GetFont(FontFactory.HELVETICA, 14, Font.BOLD);
        var phrase = new Phrase();

        SqlConnection con = new SqlConnection("Data Source = localhost; Initial Catalog = project; Integrated Security = SSPI");

        SqlCommand cm = new SqlCommand("Select lrp.fullname,  pp.profilepic, pp.rank, lrp.policeid, lrp.nric, lrp.email, lrp.contact, lrp.address,lrp.location, pp.achievement, pp.medal1, pp.medal2, pp.medal3, pp.medal4, pp.medal5 from LoginRegisterPolice lrp, PoliceProfile pp where lrp.policeid = '" + DDLCase.SelectedValue + "' and pp.policeid = lrp.policeid", con);
        con.Open();
        SqlDataReader dr;

        dr = cm.ExecuteReader();

        if (dr.Read())
        {
            phrase.Add(new Chunk("Full Name :", normalFont));
            phrase.Add(dr[0].ToString());

            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);


            phrase.Add(new Chunk("Profile Picture :\u00a0", normalFont));
            Byte[] bytes1 = (Byte[])dr[1];
            iTextSharp.text.Image image1 = iTextSharp.text.Image.GetInstance(bytes1);
            image1.ScaleToFit(100f, 250f);
            Chunk imageChunk1 = new Chunk(image1, 0, 0);
            phrase.Add(imageChunk1);

            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);

            phrase.Add(new Chunk("Rank : ", normalFont));
            Byte[] bytes2 = (Byte[])dr[2];
            iTextSharp.text.Image image2 = iTextSharp.text.Image.GetInstance(bytes2);
            image2.ScaleToFit(40f, 300f);
            Chunk imageChunk2 = new Chunk(image2, 0, 0);
            phrase.Add(imageChunk2);

            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);

            phrase.Add(new Chunk("Police ID :", normalFont));
            phrase.Add(dr[3].ToString());

            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);

            phrase.Add(new Chunk("NRIC :", normalFont));
            phrase.Add(dr[4].ToString());

            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);

            phrase.Add(new Chunk("Email :", normalFont));
            phrase.Add(dr[5].ToString());

            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);

            phrase.Add(new Chunk("Contact :", normalFont));
            phrase.Add(dr[6].ToString());

            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);

            phrase.Add(new Chunk("Address :", normalFont));
            phrase.Add(dr[7].ToString());

            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);

            phrase.Add(new Chunk("Location :", normalFont));
            phrase.Add(dr[8].ToString());

            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);

            phrase.Add(new Chunk("Achievement :", normalFont));
            phrase.Add(dr[9].ToString());

            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);



            phrase.Add(new Chunk("Medal1", normalFont));
            Byte[] bytes3 = (Byte[])dr[10];
            iTextSharp.text.Image image3 = iTextSharp.text.Image.GetInstance(bytes3);
            image3.ScaleToFit(800f, 800f);
            Chunk imageChunk3 = new Chunk(image3, 0, 0);
            phrase.Add(imageChunk3);


            phrase.Add(new Chunk("Medal2", normalFont));
            Byte[] bytes5 = (Byte[])dr[11];
            iTextSharp.text.Image image5 = iTextSharp.text.Image.GetInstance(bytes5);
            image5.ScaleToFit(800f, 800f);
            Chunk imageChunk5 = new Chunk(image5, 0, 0);
            phrase.Add(imageChunk5);



            phrase.Add(new Chunk("Medal3", normalFont));
            Byte[] bytes6 = (Byte[])dr[12];
            iTextSharp.text.Image image6 = iTextSharp.text.Image.GetInstance(bytes6);
            image6.ScaleToFit(800f, 800f);
            Chunk imageChunk6 = new Chunk(image6, 0, 0);
            phrase.Add(imageChunk6);          

            phrase.Add(new Chunk("Medal4", normalFont));
            Byte[] bytes7 = (Byte[])dr[13];
            iTextSharp.text.Image image7 = iTextSharp.text.Image.GetInstance(bytes7);
            image7.ScaleToFit(800f, 800f);
            Chunk imageChunk7 = new Chunk(image7, 0, 0);
            phrase.Add(imageChunk7);

            phrase.Add(new Chunk("Medal5", normalFont));
            Byte[] bytes8 = (Byte[])dr[14];
            iTextSharp.text.Image image8 = iTextSharp.text.Image.GetInstance(bytes8);
            image8.ScaleToFit(800f, 800f);
            Chunk imageChunk8 = new Chunk(image8, 0, 0);
            phrase.Add(imageChunk8);

            table.AddCell(phrase);


        }

        dr.Close();
        doc1.Add(table);
        doc1.Close();
        btnPDF.Enabled = false;
    }
}

【问题讨论】:

  • 我不明白您的网络应用为什么/如何为用户保存文件。当我访问一个网站时请求文件并选择它们的保存位置。服务器未经我的许可在 我的 机器上放置一些东西将是安全漏洞。我是不是误会了什么?
  • 这是我第一次尝试在我的 web 应用程序中使用 iTextSharp。因此,我实际上不太确定如何选择用户希望将文件保存在哪里:/
  • 创建 PDF 后,您只需将这些字节推送给客户端,并带有一些标题,让客户端知道如何处理它们。如果您要在服务器上创建文件,则只需执行 Response.WriteFile() 并添加 PDF 标题。如果你有一个原始字节数组,你可以做同样的事情,但使用Response.BinaryWrite()。搜索这些会给你一些更好的例子。你说的是这个吗?
  • 我已经编辑了关于我的 PDF 如何工作的问题。老实说,最初我不知道 response.writefile() 在生成 PDF 时是否有效。正如我所研究的那样,文件流实际上将文件保存到服务器中。而且我没有任何用于 response.binarywrite() 的原始字节数组。每当客户端单击按钮时,它只是直接保存到硬编码目录中,这是一个安全漏洞。因此,我想知道如何在计算机和手机上执行“另存为”功能以及通用文件目录。我让你忍受我,因为这是我第一次使用 iTextSharp 和 c#

标签: c# asp.net pdf itextsharp


【解决方案1】:

好的,根据您的 cmets,我希望这对您有所帮助。

iTextSharp.text.pdf.PdfWriter.GetInstance()的第二个参数实际上是一个通用的System.IO.Stream对象。这意味着您可以传递从该对象继承的任何对象。有几种类型可以,但在这种情况下最有用的两种是您通常使用的System.IO.FileStreamSystem.IO.MemoryStreamMemoryStream 类基本上就像一个“内存文件”,但幸运的是,您甚至不必考虑这一点。从您的角度来看,使用 MemoryStream 的唯一区别是,当您关闭 Document 时,您需要提取字节,这可以通过一行简单的方式完成。

下面是展示这一点的示例代码。首先它创建一个空的字节数组,我们稍后将在其中存储我们的 PDF。然后创建一个 MemoryStream 而不是 FileStream。然后它会创建一个非常简单的 PDF。完成后,它将 MemoryStream 中的字节转储到我们在上面创建的字节数组中。之后 iTextSharp 不再参与,我们只处理 ASP.Net。

(旁注:不是写到MemoryStream,而是提取字节并将它们发送到浏览器,你会经常看到人们直接写到Response.OutputStream。这是100%有效但我建议不要这样做因为它可能更难调试。我更喜欢先做 iTextSharp 工作并调试任何错误,然后处理 ASP.Net 和浏览器问题。)

protected void btnPDF_Click(object sender, EventArgs e) {
    //Byte array that will eventually hold our PDF, currently empty
    Byte[] bytes;

    //Instead of a FileStream we'll use a MemoryStream
    using (var MS = new System.IO.MemoryStream()) {

        //Standard PDF setup, iText doesn't care what type of stream we're using
        var doc = new iTextSharp.text.Document();
        var writer = iTextSharp.text.pdf.PdfWriter.GetInstance(doc, MS);
        doc.Open();
        doc.Add(new iTextSharp.text.Paragraph("Hello world"));
        doc.Close();

        //Grab the raw bytes from the MemoryStream
        bytes = MS.ToArray();
    }



    //At this point all iText work is done and we're only dealing with raw ASP.Net parts

    //Clear the current response buffer
    Response.Clear();
    //Instead of a normal text/html header tell the browser that we've got a PDF
    Response.ContentType = "application/pdf";
    //Tell the browser that you want the file downloaded (ideally) and give it a pretty filename
    Response.AddHeader("content-disposition", "attachment;filename=MySampleFile.pdf");
    //Write our bytes to the stream
    Response.BinaryWrite(bytes);
    //Close the stream (otherwise ASP.Net might continue to write stuff on our behalf)
    Response.End();
}

此外,这会创建一个用户浏览器应该下载的文件。但是,根据浏览器以及用户如何配置他们的浏览器,您可能会得到不同的结果。例如,我机器上的谷歌浏览器会立即将文件下载到我之前设置的当前下载文件夹中。另一方面,Mozilla Firefox 会提示我选择要保存文件的位置。 (我的 Internet Explorer 出于某种原因今天不想与 localhost 对话,所以我不确定它会做什么。)

【讨论】:

  • 我在本地主机上尝试过,但浏览器没有任何响应。当我使用 chrome 和 firefox 时,单击按钮时什么也没发生。但是,当我使用 IE 时,他们给了我这个错误,imgur.com/kdxVOvd
  • 我尝试在 azure 中将其联机并在 IE 上运行它,我收到了这个奇怪的错误 imgur.com/TnDJMy3
  • 您可以尝试添加一个新页面,添加一个按钮并使用上面的代码吗?您遇到了一些 Ajax 处理程序的问题。
  • 你是对的。但是,为什么会出现 ajax 处理程序问题?
  • 我对微软的Ajax平台了解不多,因为我不使用它。但我相信该按钮在 Ajax 回调中执行,该回调应该返回 JSON(或可能是 HTML)。此方法返回 PDF,Ajax 库无法确定如何处理它。最简单的解决方案是“取消 Ajax”这个按钮。不幸的是,我无法告诉你如何做到这一点。
【解决方案2】:

您可以使用相对路径代替绝对路径。

【讨论】:

  • 我曾尝试使用此“~/Downloads”,但它会将我定向到我的 IISExpress 文件夹,这不是我想要的。
【解决方案3】:

您也许可以使用环境变量来执行此操作。这应该让您始终获得用户文档(和下载)文件夹,无论它位于何处。

How to get temporary folder for current user

How do I get and set Environment variables in C#?

【讨论】:

  • 我仍然对代码的工作方式感到困惑,尤其是在环境方面。
【解决方案4】:

使用文件流保存到移动设备可能会出现问题,我会改用本地存储,应该适用于 PC 和移动设备。开始的东西:http://msdn.microsoft.com/en-us/hh562072.aspx,否则只需搜索 localstorage 和 webapp

【讨论】:

  • 说实话,我帮不上什么忙。也许一个很好的解释就是这样。
猜你喜欢
  • 2015-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-28
  • 1970-01-01
  • 2013-07-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多