如何使用 C# 将 pdf 文件插入和读取到 Sql Server 2005 数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/572525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert and read a pdf file to Sql Server 2005 database using C#
提问by
How to insert the pdf file into sqlserver 2005 and read the pdf file from sqlserver?
如何将pdf文件插入sqlserver 2005并从sqlserver读取pdf文件?
回答by PiRX
If you are interested into using database for file storage, look at this 4guysfromrolla article. It's web oriented, but there should be no problem finding what you need.
如果您对使用数据库进行文件存储感兴趣,请查看这篇 4guysfromrolla 文章。它是面向网络的,但找到您需要的应该没有问题。
回答by Marc Gravell
Essentially, you are just talking about BLOB storage and retrieval (of image
or varbinary(max)
data). See this question: Streaming directly to a database
本质上,您只是在谈论 BLOB 存储和检索(image
或varbinary(max)
数据)。看到这个问题:Streaming direct to a database
回答by MGOwen
To put it into the database you must read it into a byte array. Either read it from the file system or use the AspNetFileUploadWebControl.FileBytes property. Create an insert stored procedure and add the byte array as the parameter for your DB column (the column must be of SQL data type "image").
要将其放入数据库,您必须将其读入字节数组。从文件系统读取它或使用 AspNetFileUploadWebControl.FileBytes 属性。创建一个插入存储过程并添加字节数组作为数据库列的参数(该列必须是 SQL 数据类型“image”)。
To get it out of the database, use something like:
要将其从数据库中取出,请使用以下内容:
theRow = getDatarowFromDatabase();
aByteArrayOfTheFile = (byte[])theRow["theSqlImageColumnWithTheFileInIt"];
To allow the user to view or download it use my method SendAsFileToBrowser():
要允许用户查看或下载它,请使用我的方法 SendAsFileToBrowser():
SendAsFileToBrowser(aByteArrayOfTheFile, "application/pdf", "downloaded.pdf");
The source code for the method (with overloads):
该方法的源代码(带有重载):
// Stream a binary file to the user's web browser so they can open or save it.
public static void SendAsFileToBrowser(byte[] File, string Type, string FileName)
{
string disp = "attachment";
if (string.IsNullOrEmpty(FileName))
{
disp = "inline";
}
// set headers
var r = HttpContext.Current.Response;
r.ContentType = Type; // eg "image/Png"
r.Clear();
r.AddHeader("Content-Type", "binary/octet-stream");
r.AddHeader("Content-Length", File.Length.ToString());
r.AddHeader("Content-Disposition", disp + "; filename=" + FileName + "; size=" + File.Length.ToString());
r.Flush();
// write data to requesting browser
r.BinaryWrite(File);
r.Flush();
}
//overload
public static void SendAsFileToBrowser(byte[] File, string Type)
{
SendAsFileToBrowser(File, Type, "");
}
// overload
public static void SendAsFileToBrowser(System.IO.Stream File, string Type, string FileName)
{
byte[] buffer = new byte[File.Length];
int length = (int)File.Length;
File.Write(buffer, 0, length - 1);
SendAsFileToBrowser(buffer, FileName, Type);
}
// Stream a binary file to the user's web browser so they can open or save it.
public static void SendAsFileToBrowser(byte[] File, string Type, string FileName)
{
string disp = "attachment";
if (string.IsNullOrEmpty(FileName))
{
disp = "inline";
}
// set headers
var r = HttpContext.Current.Response;
r.ContentType = Type; // eg "image/Png"
r.Clear();
r.AddHeader("Content-Type", "binary/octet-stream");
r.AddHeader("Content-Length", File.Length.ToString());
r.AddHeader("Content-Disposition", disp + "; filename=" + FileName + "; size=" + File.Length.ToString());
r.Flush();
// write data to requesting browser
r.BinaryWrite(File);
r.Flush();
}
//overload
public static void SendAsFileToBrowser(byte[] File, string Type)
{
SendAsFileToBrowser(File, Type, "");
}
// overload
public static void SendAsFileToBrowser(System.IO.Stream File, string Type, string FileName)
{
byte[] buffer = new byte[File.Length];
int length = (int)File.Length;
File.Write(buffer, 0, length - 1);
SendAsFileToBrowser(buffer, FileName, Type);
}