本文介绍了如何插入和使用C#阅读PDF文件,SQL Server 2005数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以说,如何将PDF文件插入到2005年SQLSERVER和读取SQLSERVER ...

can anyone say how to insert the pdf file into sqlserver 2005 and read the pdf file from sqlserver...

谢谢advace

推荐答案

要放入数据库,则必须读入一个字节数组。无论是从文件系统读取或使用的AspNetFileUploadWebControl.FileBytes属性。创建插入存储过程并添加字节数组作为参数为您的数据库列(列必须是SQL数据类型形象)。

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"];

要允许用户查看或下载使用我的方法SendAsFileToBrowser():

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);
	}



这篇关于如何插入和使用C#阅读PDF文件,SQL Server 2005数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 02:28