首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用c#将生成的pdf保存在数据库中

使用c#将生成的pdf保存在数据库中
EN

Stack Overflow用户
提问于 2013-12-16 17:30:29
回答 2查看 3.9K关注 0票数 1

我正在开发asp.net应用程序,它以多种形式接受用户的输入,并以pdf格式保存。现在我被困在不知道如何将生成的pdf保存在Server数据库中的地方。

在这里,我是附加代码,以保存在pdf。

代码语言:javascript
复制
protected void btnExportPDF_Click(object sender, EventArgs e)
{
   Response.ContentType = "application/pdf";
   Response.AddHeader("content-disposition", "attachment;filename=Samba.pdf");
   Response.Cache.SetCacheability(HttpCacheability.NoCache);

   StringWriter st = new StringWriter();
   HtmlTextWriter ht = new HtmlTextWriter(st);

   gvExperience.AllowPaging = false;
   gvExperience.DataBind();
   gvExperience.RenderControl(ht);

   StringReader sr1 = new StringReader(st.ToString());
   Document pdfDoc = new Document(PageSize.A4, 10f,10f,10f,0f);

   HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
   PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
   pdfDoc.Open();
   htmlparser.Parse(sr1);
   pdfDoc.Close();

   Response.Write(pdfDoc);
   Response.End();  
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-12-16 17:42:33

创建一个至少包含文件名(varchar(100)、内容(varbinary(max))和内容长度(int)的表。

将pdf转换成字节数组(byte[])。我会将您的Reponse.OutputStream交换为一个内存流。使用首选的数据访问方法保存到Server。

server的“varbinary”将转换为C#的“byte[]”。当您将其写回客户端浏览器时,您将希望设置“content”标题,并使用“Response.BinaryWrite(byte[])”写入流。

票数 4
EN

Stack Overflow用户

发布于 2013-12-16 17:44:36

转换为base64字符串,然后插入到DB中。

代码语言:javascript
复制
byte[] pdfBytes = File.ReadAllBytes(pdfPath);
string pdfBase64 = Convert.ToBase64String(pdfBytes);

编码愉快。:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20616981

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档