Well, to upload to the web-server you just need an <input type="file" .../>
. At ASP.NET, this should be available in the Request.Files
collection.
In SQL Server, you'll want a varbinary(max)
(or image
in older versions).
The big question before pushing them into SQL Server is: how big are they? If they aren't massive, you can just treat them as a byte[]
; just buffer from the InputStream
and send. Here's a fairly generic way of buffering a Stream
to a single byte[]
:
byte[] buffer = new byte[1024];
int bytesRead;
MemoryStream ms = new MemoryStream();
while ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, bytesRead);
}
byte[] data = ms.ToArray();
For massive files (which perhaps don't belong in the db unless you are using the SQL 2008 filestream extensions), you'll want to chunk it. Here's an old reply I used for chunking (before SQL 2005) - but it shows the overall idea.