0

I have a field in database of type image which stores the file in binary format.

Now I want to give a facility to user from where the user can downlaod that file. I have the file data in byte array object. How can I display the open save dialoge box to user?

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
Rohit Vyas
  • 1,949
  • 3
  • 19
  • 28

3 Answers3

0

This is an example to show blob images from the database, you can modify it to offer the file for download instead of showing the image (id is the image file id in the database):

void  CreateImage(string id)
{
    // Connection string is taken from web.config file.
    SqlConnection _con = new SqlConnection(
      System.Configuration.ConfigurationSettings.AppSettings["DB"]);

    try
    {
        _con.Open();
        SqlCommand _cmd = _con.CreateCommand();
        _cmd.CommandText = "select logo from" + 
                           " pub_info where pub_id='" + 
                           id + "'";
        byte[] _buf = (byte[])_cmd.ExecuteScalar();

        // This is optional
        Response.ContentType = "image/gif";

        //stream it back in the HTTP response
        Response.BinaryWrite(_buf);


    }
    catch
    {}
    finally
    {
        _con.Close();

    }
}
default
  • 11,485
  • 9
  • 66
  • 102
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
0

Try to see this: Create ashx file handler serving the image with header:

Content-Type: application/force-download
Fanda
  • 3,760
  • 5
  • 37
  • 56
0

What about:

//using System.Net;   
WebClient wc = new WebClient();

OpenFileDialog ofd = new OpenFileDialog();
if(ofd.ShowDialog() == DialogResult.OK)
   wc.DownloadFile("http://website.net/image.jpg", ofd.FileName);

EDIT:

//using System.IO;
MemoryStream ms = new MemoryStream(array);
Bitmap bmp = new Bitmap(ms);
bmp.Save("C:\\image.bmp");
Omar
  • 16,329
  • 10
  • 48
  • 66
  • Hi Fuex thanks for your reply, actully I am working on a silverlight applcation and I am having the file object in byte[] array. – Rohit Vyas Oct 30 '12 at 09:33
  • can you show some code which I can use in silverlight applicaiton? – Rohit Vyas Oct 30 '12 at 09:42
  • Look here for [OpenFileDialog in Silverlight](http://msdn.microsoft.com/en-us/library/system.windows.controls.openfiledialog(v=vs.95).aspx) and use the edited part of my answer to save an image from an array of bytes. – Omar Oct 30 '12 at 15:13