SQL Server Upload Sample
To see this sample in action, download the SlickUpload package and configure it for your environment.
NOTE: the maximum allowed request size for this sample is 1000 MB. If you attempt to upload files larger than this,
you will recieve a oversized upload error which SlickUpload will handle gracefully. This is controlled by the maxRequestLength attribute of the
httpRuntime key in the web.config file.
Configuration
Configuration for the SQL sample involves creating the file database and table and configuring SlickUpload
to point at that database. To do this, perform the following steps:
- Create a new database, or select an existing database to use
- Open and run the SlickUploadFile.sql script in the SlickUpload distribution package root folder on the selected database
- Change the connection string in the uploadStreamProvider web.config section to point to the database selected above
Description
SlickUpload includes the capability to upload streaming directly to a SQL database, eliminating any in-memory copy of files as they are uploaded. This
is accomplished by using the SqlClient UploadStreamProvider. To use this instead of the default File UploadStreamProvider, use an uploadStreamProvider web.config
configuration section like the following:
<uploadStreamProvider provider="SqlClient"
connectionString="server=.;uid=xxx;pwd=xxx;database=xxx;"
table="SlickUploadFile"
keyField="FileId" dataField="Data" fileNameField="FileName" />
You can use other table and field names by changing them in the database and setting them in the SlickUpload configuration. This configuration is the default,
built around the provided SlickUploadFile.sql schema. Once the files are uploaded, you can update the records with additional info, select, manipulate, etc. just
like any other database record.
To retrieve the data from the record while still maintaining the streaming interaction, use the SqlClientOutputStream class. Create an instance and read the data out from the stream,
manipulating it or sending it to the client as you go. For example, here's how it is used in this sample:
public Stream GetDataStream()
{
return new SqlClientOutputStream(_cnString, _table, _dataField, _keyField, _id);
}
The download file handler uses this method to get a stream, and then streams the file down to the client:
public void ProcessRequest(HttpContext context)
{
int id = int.Parse(context.Request.QueryString["id"]);
RepositoryFileCS file = RepositoryFileCS.GetById(id);
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
context.Response.AddHeader("Content-Length", file.Length.ToString());
context.Response.ContentType = "application/octet-stream";
using (Stream dataStream = file.GetDataStream())
{
byte[] buffer = new byte[8192];
int read;
while ((read = dataStream.Read(buffer, 0, 8192)) > 0)
context.Response.OutputStream.Write(buffer, 0, read);
}
}
|