我正在开发asp.net应用程序,它以多种形式接受用户的输入,并以pdf格式保存。现在我被困在不知道如何将生成的pdf保存在Server数据库中的地方。
在这里,我是附加代码,以保存在pdf。
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();
}发布于 2013-12-16 17:42:33
创建一个至少包含文件名(varchar(100)、内容(varbinary(max))和内容长度(int)的表。
将pdf转换成字节数组(byte[])。我会将您的Reponse.OutputStream交换为一个内存流。使用首选的数据访问方法保存到Server。
server的“varbinary”将转换为C#的“byte[]”。当您将其写回客户端浏览器时,您将希望设置“content”标题,并使用“Response.BinaryWrite(byte[])”写入流。
发布于 2013-12-16 17:44:36
转换为base64字符串,然后插入到DB中。
byte[] pdfBytes = File.ReadAllBytes(pdfPath);
string pdfBase64 = Convert.ToBase64String(pdfBytes);编码愉快。:)
https://stackoverflow.com/questions/20616981
复制相似问题