Reputation: 84
Unable to download image in user side downloads folder . i have tried a number of ways to solve the problem.I have also given the file path of user and trying to download image presented in asp:image control "url" ... Whole code executed but didn't saved image in folder.I have tries more solution but didn't worked.any suggestions to help me out... i am stuck at this..thank you in advance...
//To show image from URl on asp:image control on click of gridview button
protected void Redirect1_Click(object sender, EventArgs e)
{
var rowIndex = ((GridViewRow)((Control)sender).NamingContainer).RowIndex;
BookingNO = GridView1.Rows[rowIndex].Cells[0].Text;
var request = WebRequest.Create("http:-#-//?a=" + BookingNO + "&b=2" + "");
using (var response = request.GetResponse())
using (var stream = response.GetResponseStream())
{
LoadImage.ImageUrl = "http:-#-//?a=" + BookingNO + "&b=2" + "";
LoadImage.Visible = true;
md.Visible = true;
LoadImage.ToolTip = "Image of Order Number. " + BookingNO + "";
Download.Visible = true;
}
}
//Button Click to download image of
public void Download_click(object sender, EventArgs e)
{
try
{
string pathUser = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
string pathDownload = Path.Combine(pathUser, "Downloads");
string url = LoadImage.ImageUrl;
byte[] data;
using (WebClient client = new WebClient())
{
data = client.DownloadData(url);
}
File.WriteAllBytes(pathDownload + BookingNO + ".jpg", data);
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Order Number " + BookingNO + " Image Saved');", true);
}
catch (Exception ex)
{
}
}
Upvotes: 0
Views: 304
Reputation: 156958
You can't force saving a file on the client computer, and definitely not in a specific folder. You can only hand over the file to the user and let the browser save it or ask the user where and whether to save it.
Your code doesn't download a single image by the way, it sets the Image control's source location at best.
If you want to write an image to the output stream (when you have the image as byte array), then you can do this:
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=\"file.png\"");
HttpContext.Current.Response.OutputStream.Write(imageBytes, 0, imageBytes.Length);
Upvotes: 1