2

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

4

1 回答 1

2

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

数据类型的 DB 查询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;
        }
    }
}
于 2010-10-26T22:01:55.240 回答