Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用c在数据库中保存生成的pdf#_C#_Asp.net_Sql_Sql Server_Pdf - Fatal编程技术网

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

C# 使用c在数据库中保存生成的pdf#,c#,asp.net,sql,sql-server,pdf,C#,Asp.net,Sql,Sql Server,Pdf,我正在开发asp.net应用程序,它以多种形式接收用户的输入,并以pdf格式保存。现在我被困在不知道如何将生成的pdf保存到SQL Server数据库中的地方 有了这个,我附加了保存在pdf中的代码 protected void btnExportPDF_Click(object sender, EventArgs e) { Response.ContentType = "application/pdf"; Response.AddHeader("content-dispositio

我正在开发asp.net应用程序,它以多种形式接收用户的输入,并以pdf格式保存。现在我被困在不知道如何将生成的pdf保存到SQL 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();  
}

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

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


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

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

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


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

转换为base64字符串,然后插入数据库

byte[] pdfBytes = File.ReadAllBytes(pdfPath);
string pdfBase64 = Convert.ToBase64String(pdfBytes);

快乐编码:)

转换为base64字符串,然后插入数据库

byte[] pdfBytes = File.ReadAllBytes(pdfPath);
string pdfBase64 = Convert.ToBase64String(pdfBytes);

快乐编码:)

您可能希望将.pdf保存到文件系统,并保存指向数据库中资源的字符串路径。你尝试过什么?或者..使用谷歌并找到这个:不要假设我们知道你在使用什么数据库。您使用的是SQL Server、Oracle、MySQL还是其他什么?无论数据库是什么,您都有两个选择。将其保存在文件系统中,只需保存到数据库的路径即可。或者将其另存为BLOB。@DGibbs,我正在尝试生成pdf并直接保存到数据库,而不保存到本地磁盘。现在,我刚刚成功地生成了pdf并将其保存到本地磁盘。@Andrew,我遇到过这个解决方案,但我仍然坚持使用,因为我计划生成pdf并直接保存到数据库,而不保存在本地磁盘上。您可能希望将.pdf保存到文件系统,并保存指向数据库中资源的字符串路径。你尝试过什么?或者..使用谷歌并找到这个:不要假设我们知道你在使用什么数据库。您使用的是SQL Server、Oracle、MySQL还是其他什么?无论数据库是什么,您都有两个选择。将其保存在文件系统中,只需保存到数据库的路径即可。或者将其另存为BLOB。@DGibbs,我正在尝试生成pdf并直接保存到数据库,而不保存到本地磁盘。现在,我刚刚成功地生成了pdf并将其保存到本地磁盘。@Andrew,我遇到过这个解决方案,但我仍然坚持使用,因为我计划生成pdf并直接保存到数据库,而不保存在本地磁盘上。是否可以帮助我对上述部分进行编码,因为我对这一点完全不熟悉。我不能给你代码,但是你需要System.IO.MemoryStream。类似于'var stream=newmemoryStream();{write to stream}var bytes=stream.ToArray();'。至于您的数据访问,为了简单起见,我只需要实现一个实体框架上下文。创建数据库和模式后,Entity Framework提供了自动导入模式并将其映射到代码的机制。谢谢,终于理解了上述内容..:我不可能帮助我为上面提到的部分编码,因为我对这一部分完全不熟悉。我不能给你编码,但是你需要System.IO.MemoryStream。类似于'var stream=newmemoryStream();{write to stream}var bytes=stream.ToArray();'。至于您的数据访问,为了简单起见,我只需要实现一个实体框架上下文。创建数据库和模式后,Entity Framework提供了自动导入模式并将其映射到代码的机制。谢谢,终于理解了上述内容..:D