Retrieving MS Word Document from Database and Saving Locally
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开发者_StackOverflow社区 a Image SQL Server data type. Thanks
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;
}
}
}
精彩评论