Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法在用户端下载文件夹中下载图像_C#_Asp.net_Image_C# 4.0_Asp.net Web Api - Fatal编程技术网

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

C# 无法在用户端下载文件夹中下载图像,c#,asp.net,image,c#-4.0,asp.net-web-api,C#,Asp.net,Image,C# 4.0,Asp.net Web Api,无法下载用户端下载文件夹中的图像。我已经尝试了很多方法来解决这个问题。我还提供了用户的文件路径,并尝试下载asp:image控件“url”中显示的图像。。。整个代码已执行,但未将图像保存在文件夹中。我尝试了更多解决方案,但未成功。是否有任何建议可帮助我解决此问题。。。 我被这件事困住了…先谢谢你 //To show image from URl on asp:image control on click of gridview button protected void Redirect1

无法下载用户端下载文件夹中的图像。我已经尝试了很多方法来解决这个问题。我还提供了用户的文件路径,并尝试下载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;

    }
}
//点击按钮下载图片
 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)
     {
     }
}

您不能强制将文件保存在客户端计算机上,而且绝对不能保存在特定文件夹中。您只能将文件交给用户并让浏览器保存,或询问用户保存位置和是否保存

顺便说一下,您的代码不会下载一个图像,它最多只能设置图像控件的源位置

如果要将图像写入输出流(当图像作为字节数组时),则可以执行以下操作:

HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=\"file.png\"");
HttpContext.Current.Response.OutputStream.Write(imageBytes, 0, imageBytes.Length);

你能给我具体的代码或示例代码让我试试。具体做什么?实际上我是新手。谢谢你的快速回复。我的答案仍然适用。让我试试你的。