开发者

how to download an image stored in sql server

I am working on a web application where have stored images in sql server DB. I have used 'Image' datatype to store the images in the table. When user clicks on a button i want to give "open with/save as" option. How to do that in c# asp.net?

the code that i am using is below

 conn = connect.getConnection();
 selcmd = new System.Data.SqlClient.SqlCommand("select image from tblQuestion where qid=" + context.Request.QueryString["qid"]开发者_JAVA技巧, conn);
          conn.Open();
          rdr = selcmd.ExecuteReader();
          while (rdr.Read())
          {
            context.Response.ContentType = "image/jpg";
            context.Response.AddHeader("content-disposition","attachment;filename=Tr.png");
            context.Response.BinaryWrite((byte[])rdr["image"]);

          }

But still its not working


  1. Set Response.ContentType to the relevant mime type for the image (eg "image/jpg")
  2. Set the content-disposition header to "attachment" so that the user is prompted to save the file
  3. Use Response.BinaryWrite method to write the image bytes to the page output stream

As you can see there's a more to it than meets the eye, hopefully those key points plus the power of google will get you going :)


Check this links to get the images from Data base

GettingFromDB

Geeting from the Path saved

This is the one you required

Image


You have to convert the image to byte array. Use the following code for the casting:

           if (dataReader["Image"] != DBNull.Value)
            {
                byte[] storedImage = (byte[])(dataReader["Image"]);
                orderMaster.Photo = storedImage;
            }

Here the image field is in the type Image in DB.

Then using system.Drawing create the image from the byte array and point the location to the control in which you show image.

    private void byteArrayToImage(byte[] byteArray)
    {
        if (byteArray != null)
        {
            MemoryStream ms = new MemoryStream(byteArray);
            System.Drawing.Image img = System.Drawing.Image.FromStream(ms, false,  false);
            /*last argument is supposed to turn Image data validation off*/
            img.Save(Server.MapPath("Photo/image.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);
            ms.Close();
            //Image1.ImageUrl = Server.MapPath("Photo/image.jpg");
            imgViewPhoto.ImageUrl = ConfigurationManager.AppSettings["PhotoFolder"].ToString() + "Photo/image.jpg";   
        }
    }

Hope this will help you..


Check this code.. i will store anything in database and retrieve in from database..

    Dim con As New SqlConnection
    con.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
    con.Open()

    Dim objbytes() As Byte
    ReDim objbytes(FileUpload1.PostedFile.InputStream.Length)
    ' Dim query As String = "insert into files(files,ext) values('" & objbytes & "','" & FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf(".") + 1) & "'"
    Dim query As String = "insert into files(files,ext) values(@files,@ext)"
    FileUpload1.PostedFile.InputStream.Read(objbytes, 0, FileUpload1.PostedFile.InputStream.Length)

    Dim cmd As New SqlCommand(query, con)

    cmd.Parameters.AddWithValue("@files", objbytes)
    cmd.Parameters.AddWithValue("@ext", FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf(".") + 1))
    cmd.ExecuteNonQuery()
    'FileUpload1.PostedFile.InputStream.Read(objbytes, 0, FileUpload1.PostedFile.InputStream.Length)
    Dim ds As New DataSet
    Dim adp As New SqlDataAdapter("select files,ext from files", con)
    adp.Fill(ds)
    For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
        ' Dim enc As System.Text.Encoding = System.Text.Encoding.ASCII

        'Dim objbyte1s() As Byte = enc.GetBytes(ds.Tables(0).Rows(i).Item(0))

        System.IO.File.WriteAllBytes(Server.MapPath("~/files/file1." & ds.Tables(0).Rows(i).Item(1)), ds.Tables(0).Rows(i).Item(0))
        'Response.BinaryWrite(ds.Tables(0).Rows(i).Item(0)))
    Next
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜