【问题标题】:Retrieving MS Word Document from Database and Saving Locally从数据库中检索 MS Word 文档并在本地保存
【发布时间】:2010-10-26 21:53:10
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    网络表单中最好的方法是使用 HTTP 处理程序。

    image 数据类型的 DB 查询将映射到 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;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-28
      • 2019-07-07
      • 1970-01-01
      • 2019-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-31
      相关资源
      最近更新 更多