ASP.NET - DownLoad image and location Path - Asked By msakt on 27-Jun-12 12:29 AM

private void download (DataTable dt)
{
    Byte[] bytes = (Byte[])dt.Rows[0]["Data"];
    Response.Buffer = true;
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = dt.Rows[0]["ContentType"].ToString();
    Response.AddHeader("content-disposition", "attachment;filename="+ dt.Rows[0]["Name"].ToString());
    Response.BinaryWrite(bytes);
    Response.Flush();
    Response.End();
}
the above code is working fine..but download image is store in downloads....i Want ,when i click save ask location path for save downloading image..

how?
S K replied to msakt on 27-Jun-12 12:47 AM

Yes when you are using BinaryWrite that is stream a byte array or write the output directly. 

The way to do this is to use http://msdn2.microsoft.com/en-us/library/12s31dhy%28vs.80%29.aspx to explicitly send the file from your ASP.NET application and then add the Content Type and Content-Disposition headers.

For example:


Response.ContentType = "image/jpeg";
Response.AppendHeader("Content-Disposition","attachment; filename=SailBig.jpg");
Response.TransmitFile( Server.MapPath("~/images/sailbig.jpg") );
Response.End();

This will cause a Open / Save As dialog box to pop up with the filename of SailBig.jpg as the default filename preset.

hope this helps you

Neha Garg replied to msakt on 27-Jun-12 12:55 AM
Hi...

See the below code for save dialog box and desired location:


string FileName = "";
SaveFD1.InitialDirectory = "C:";
SaveFD1.FileName = "";
SaveFD1.Title = "Save File As";
SaveFD1.Filter = "Text Files|*.txt|All Files|*.xls|*.*";
if (SaveFD1.ShowDialog() == DialogResult.OK)
{
FileName = SaveFD1.FileName;
 
richTextBox1.SaveFile(FileName, RichTextBoxStreamType.PlainText);
}

Jitendra Faye replied to msakt on 27-Jun-12 01:13 AM
Use this code -

string filepath ="path";

            byte[] data = System.IO.File.ReadAllBytes(filepath);
            Response.Clear();
            Response.ClearHeaders();
            Response.AddHeader("Content-Type", "Application/octet-stream");
            Response.AddHeader("Content-Length", data.Length.ToString());
            Response.AddHeader("Content-Disposition", "attachment; filename=" + filepath);
            Response.BinaryWrite(data);
            Response.End();


Try this and let me know;\