How to insert the pdf file into sqlserver 2005 and read the pdf file from sqlserver?
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").
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(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);
}