I have used AsyncFileUpload AJAX control to upload a file to a column in a SQL Server database using LINQ to SQL. How do I retrieve the document from the database and allow the user to save to local drive using a Save As Dialog box using LINQ to SQL? This is ASP.NET web application. The DocumentFileContent database column is a Image SQL Server data type. Thanks
+2
A:
The best way in webforms is to use an HTTP handler.
The DB query for an image
datatype will map to a byte[]
.
public class Document : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
using (SQLConnection con = new SQLConnection)
{
SqlCommand command = new SqlCommand("SELECT imagefield FROM table", con);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
context.Response.ContentType = "application/msword";
context.Response.BinaryWrite(reader["imagefield"]);
}
}
reader.Close();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
Dustin Laine
2010-10-26 22:01:55