C# 使用contenttype在弹出窗口中打开varbinary列

C# 使用contenttype在弹出窗口中打开varbinary列,c#,asp.net,webmethod,C#,Asp.net,Webmethod,我正在使用html标签制作一个文件上传程序。我的表中有id、名称和数据。数据以二进制格式将文件内容存储在数据类型为varbinary(max)的列中。我能够成功上传文件,我也能够读回字节数组 代码隐藏处的上载代码: protected void Upload(object sender, EventArgs e) { string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);

我正在使用html标签制作一个文件上传程序。我的表中有id、名称和数据。数据以二进制格式将文件内容存储在数据类型为
varbinary(max)
的列中。我能够成功上传文件,我也能够读回字节数组

代码隐藏处的上载代码:

    protected void Upload(object sender, EventArgs e)
    {
        string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
        string contentType = FileUpload1.PostedFile.ContentType;

        using (Stream fs = FileUpload1.PostedFile.InputStream)
        {
            using (BinaryReader br = new BinaryReader(fs))
            {
                byte[] bytes = br.ReadBytes((Int32)fs.Length);
                string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constr))
                {
                    string query = "insert into FileUploader2 values (@Name, @ContentType, @Data)";
                    using (SqlCommand cmd = new SqlCommand(query))
                    {
                        cmd.Connection = con;
                        cmd.Parameters.AddWithValue("@Name", filename);
                        cmd.Parameters.AddWithValue("@ContentType", contentType);
                        cmd.Parameters.AddWithValue("@Data", bytes);
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                        Context.Response.Write("Uploaded Successfully!");
                    }
                }
            }
        }

        Response.Redirect(Request.Url.AbsoluteUri);
    }
我还创建了一个网格,显示文件列表以及一个图标,单击该图标将在读取字节数组后查看文件。我在onclick上有一个JS函数,我在代码隐藏处通过WebMethod访问它。以下是用户单击视图图标时的代码:

    [System.Web.Services.WebMethod]
    public static void ShowDocument()
    {
        string filename = string.Empty;
        string contentType = string.Empty;
        byte[] bytes = null;
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            con.Open();

            using (SqlCommand com = new SqlCommand("SELECT * FROM FileUploader2", con))
            {
                using (SqlDataReader reader = com.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        filename = (string)reader["Name"];
                        contentType = (string)reader["ContentType"];
                        bytes = (byte[])reader["Data"];
                    }
                }
            }
        }

        System.Web.HttpContext.Current.Response.ContentType = contentType;
        System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; 
        filename=" + filename);
        System.Web.HttpContext.Current.Response.OutputStream.Write(bytes, 0, bytes.Length);
        System.Web.HttpContext.Current.Response.Flush();
    }

此代码工作正常,可以正确读取所有列。现在在阅读之后,我想在弹出窗口中查看该文件。我怎样才能做到?我想我必须使用contenttype让它知道我想要打开什么类型的文件,然后使用字节来打开。但我不知道如何实现这一点。

您知道数据可能代表哪些内容类型吗?它将很容易显示为一个,例如,十六进制字符串。否则,您需要将其转换为任何类型,并适当地显示它。我不认为有什么神奇的解决办法对不起。我不明白你刚才说的话。“我的表格”中的ContentType保存我上载的文件类型。例如文本/普通或应用程序/八位字节流