locked
Reading varbinary(max) from database and display that file in a pop window RRS feed

  • Question

  • User-644059487 posted

     am making a file uploader using html tags. I have a column in my table Data which has a datatype varbinary(max) so files are successfully being saved in the database in binary format. I am also displaying the list of files in a grid with an icon next to it for viewing the file. When I click on that icon it should read my data which is in binary format and then view that file.

    My code for upload at code behind is :

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

    I am also able to sucessfully read my data back. The code for which is as follows:

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

    }

    After reading, how will I display that file in a pop window? I have no idea how to do it. Any help would be appreciated.

    Monday, February 17, 2020 9:57 AM

All replies

  • User665608656 posted

    Hi Jessica,

    After reading, how will I display that file in a pop window? I have no idea how to do it. Any help would be appreciated.

    Do you want to open files directly in a browser window? If so, what types of files do you have?

    Browsers have different support for opening different types of files.

    Usually, it can be opened for txt, json, pdf, etc., but for some special formats, it involves browser compatibility and is not allowed to be implemented.

    Best Regards,

    YongQing.

    Tuesday, February 18, 2020 9:51 AM
  • User-644059487 posted
    Yes I want to open files in my browser. I am opening a simple pdf or txt file. Using content disposition should do so, but its not working. I dont know why.
    Tuesday, February 18, 2020 1:07 PM
  • User665608656 posted

    Hi jessica,

    Generally speaking, to open a file in a browser, you need to download it before opening it.

    If you want to skip the download step, you need to download some plug-ins, which depends on which browser you use and their version.

    You can refer to the specific content:

    Force view text file instead of download in Firefox?

    Why .txt file in <iframe> is getting downloaded instead of displayed?

    Getting chrome to open “text” files in a tab

    Best Regards,

    YongQing.

    Wednesday, February 19, 2020 9:16 AM