从数据库中检索MS Word文档并在本地保存

时间:2010-10-26 21:53:10

标签: linq-to-sql dialog save-as

我使用AsyncFileUpload AJAX控件使用LINQ to SQL将文件上传到SQL Server数据库中的列。如何从数据库中检索文档并允许用户使用LINQ to SQL使用“另存为”对话框保存到本地驱动器?这是ASP.NET Web应用程序。 DocumentFileContent数据库列是Image SQL Server数据类型。感谢

1 个答案:

答案 0 :(得分:2)

webforms中最好的方法是使用HTTP处理程序。

image数据类型的数据库查询将映射到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;
        }
    }
}
相关问题