【问题标题】:Save image from Image.Control to website directory将图像从 Image.Control 保存到网站目录
【发布时间】:2011-12-04 17:04:36
【问题描述】:

有没有办法将图像从 .net Image Control 保存到服务器目录?

这是在 Page_Load 上生成的条形码图像:

Image1.ImageUrl = "~/app/barcode.aspx?code=TEST"

条形码图像在网页上显示得很好,但我也想将其保存到目录中:

~/barcode image/TEST.jpg


到目前为止,这是我所拥有的:

WebClient webClient = new WebClient();
string remote = Server.MapPath("..") + @"\app\barcode.aspx?code=TEST";
string local = Server.MapPath("..") + @"\barcode image\TEST.jpg";
webClient.DownloadFile(remote, local);

但它给了我一个错误:Illegal characters in path.

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    WebClient 需要远程参数的网址 - 您正在为它提供此参数的 本地 路径。

    另外,Server.MapPath 需要一个正确的目录 - 而不是 ..

    WebClient webClient = new WebClient();
    string remote = @"http://localhost/app/barcode.aspx?code=TEST";
    string local = Server.MapPath("barcode image/TEST.jpg");
    webClient.DownloadFile(remote, local);
    

    【讨论】:

    • oded - 我应该硬编码吗? @"localhost... 因为我收到错误:WebClient 请求期间发生异常。
    • @PodMays - 我不知道你的网络服务器的名称。您可以改为从 Http 标头中获取此信息。
    • oded 我能够通过:remote = @"http://" + Request.Url.Authority.ToString()+ @"/app/BarCode.aspx?code=TEST" 但它仍然给我错误。 An exception occurred during a WebClient request.
    • @PodMays - 什么例外?详情是什么?实际上,看看这些,您可能会知道出了什么问题。
    • 这就是它所说的全部内容:An exception occurred during a WebClient request。我仍然不确定有什么问题。代码在调试器中如下所示:webClient.DownloadFile("http://localhost:6585/app/BarCode.aspx?code=test", "C:\\test.jpg")
    猜你喜欢
    • 2012-08-13
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多