简体   繁体   English

无法在用户端下载文件夹中下载图像

[英]Unable to download image in user side download folder

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... 我已经尝试了多种方法来解决问题。我还给出了用户的文件路径,并尝试下载asp:image控件“ url”中显示的图像...执行了全部代码,但未将图像保存在文件夹中。我已经尝试了更多的解决方案,但是没有用。任何建议可以帮助我...我对此感到困惑。.在此先感谢您...

 //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)
     {
     }
}

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. 您的代码不会下载单个图像,而是最多设置了Image控件的源位置。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM