On Fri, Feb 6, 2009 at 8:52 PM, Pete <[email protected]> wrote:
>
> Hi,
>
> I am relatively new to ASP.NET and I'm having a little trouble
> understanding how to accomplish something. In my ASP.NET Website on
> one of the Webforms I have a File Upload Control which allows the user
> to upload a file (either .pdf or .doc file) to a Web Server. I want to
> store the byte array of the file in an SQL Server Database. When I
> upload the file from the local hard drive (say C:\Stuff.doc) to the
> web server I lose the path to the file because it's relative to the
> web server. I need the original filename or it's equivalent to create
> the filestream in the code-behind on the web server. Can someone tell
> me how to do this or what I need to do here? Your help is much
> appreciated.
>
> Thanks so much,
>
> Pete
//Just an example to insert
byte[] doc = GetFile(_filePath);
SqlConnection nwindConn = new SqlConnection("Data
Source=localhost;Integrated Security=SSPI;Initial
Catalog=Northwind;");
SqlCommand cmd = new SqlCommand("INSERT INTO FileStore ([File]) "
+ "Values(@File)", nwindConn);
cmd.Parameters.Add("@File", SqlDbType.Image, doc.Length).Value = doc;
nwindConn.Open();
cmd.ExecuteNonQuery();
nwindConn.Close();
public static byte[] GetFile(string filePath)
{
FileStream fs = new FileStream(filePath, FileMode.Open,
FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] file = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
return file;
}