【问题标题】:Displaying varbinary(max) from database using content disposition使用内容处置从数据库中显示 varbinary(max)
【发布时间】:2020-02-17 11:24:21
【问题描述】:

我正在使用 HTML 标签制作文件上传器。我的表 Data 中有一个数据类型为 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);
    }

我还能够成功地读回我的数据。其中的代码如下:

    [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();
}

现在阅读后,它还应该显示文件的内容或下载文件的链接,因为我使用的是附件。但它什么也没做。调试后它给了我正确的值。但为什么它不显示文件?我究竟做错了什么?据我所知,由于我使用的是 content-disposition,它应该会自行显示。

【问题讨论】:

    标签: c# asp.net webmethod


    【解决方案1】:

    我认为你错过了这个部分......

        string strFileName = "FileName";//Here you have to set the File Name...
    
        if (strFileName.Substring(strFileName.LastIndexOf('.') + 1).ToLower() == "pdf")
        {
            Response.ContentType = "application/PDF";
        }
        else if (strFileName.Substring(strFileName.LastIndexOf('.') + 1).ToLower() == "doc")
        {
            Response.ContentType = "application/msword";
        }
        else if (strFileName.Substring(strFileName.LastIndexOf('.') + 1).ToLower() == "docx")
        {
            Response.ContentType = "application/msword";
        }
        else if (strFileName.Substring(strFileName.LastIndexOf('.') + 1).ToLower() == "xls")
        {
            Response.ContentType = "application/vnd.ms-excel";
        }
        else if (strFileName.Substring(strFileName.LastIndexOf('.') + 1).ToLower() == "xlsx")
        {
            Response.ContentType = "application/vnd.ms-excel";
        }
        else if (strFileName.Substring(strFileName.LastIndexOf('.') + 1).ToLower() == "ppt")
        {
            Response.ContentType = "application/vnd.ms-powerpoint";
        }
        else if (strFileName.Substring(strFileName.IndexOf('.') + 1).ToLower() == "txt")
        {
            Response.ContentType = "text/plain";
        }
        else
        {
            Response.ContentType = "text/plain";
        }
    
        System.IO.FileInfo file = new System.IO.FileInfo(strFileName);
    
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
        Response.AddHeader("Content-Length", file.Length.ToString());
    
        Response.WriteFile(file.FullName);
        Response.End();
    

    编辑后,如果你的数据是以字节为单位的,请尝试最后一次:

    public void byteArrayToImage(byte[] bytes)
        {
            if (byteArray != null)
            {
                MemoryStream ms = new MemoryStream(bytes);
                System.Drawing.Image img = System.Drawing.Image.FromStream(ms, false,  false);
    
                img.Save(Server.MapPath("Photo/image.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);//Set Your image Location...
                ms.Close();                
            }
        }
    

    【讨论】:

    • 我应该把它放在我的代码中的什么地方?在响应部分之前?
    • 好吧,让我试试这个
    • 如果你的问题没有解决...你可以参考这个...stackoverflow.com/questions/25400555/…
    • string strFileName = "FileName" 这里我们必须给出我们的文件名吗?但为什么?我的文件在一个网格中,它应该会查看我选择的文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2013-02-19
    相关资源
    最近更新 更多