0

尝试设置个人图像上传/下载器网页。我已经查看了有关该功能的 msdn 文章,它运行良好,但我无法弄清楚它将文件保存到哪里。

var request = new WebClient();
request.DownloadFile("https://someabosolute/image/path/andfilename.png", "file.png");

有任何想法吗?

将 filename.png 更改为 C:\tmp\filename.png 后,@escape 或 \ 解决无效字符问题。但是,使用 jsut 文件名会导致错误一切运行,但我找不到该文件。甚至检查了 Windows 安全和应用程序日志,也没有与时间框架相关的内容。

因此,当您仅将文件名指定为 system.net.webclient 的 DownloadFile() 方法中的第二个参数时,应将文件保存到 Web 应用程序的哪个位置。

4

2 回答 2

2

根据Microsoft 文档中的示例,该文件将下载到Application.StartupPath. 如果只提供了一个文件名,它将保存在与您的可执行文件相同的路径中。

请记住,对于某些应用程序(例如 ASP.NET Web 应用程序),将文件保存在与可执行文件相同的文件夹中可能存在权限问题。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace ConsoleApplication2
{
  class Program
  {
    static void Main(string[] args)
    {
      string remoteUri = "put your url here";
      string fileName = "image.php_.jpeg", myStringWebResource = null;
      // Create a new WebClient instance.
      WebClient myWebClient = new WebClient();
      // Concatenate the domain with the Web resource filename.
      myStringWebResource = remoteUri + fileName;
      Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource);
      // Download the Web resource and save it into the current filesystem folder.
      myWebClient.DownloadFile(myStringWebResource, @"c:\tmp\" + fileName);
      Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource);
    }
}

}

于 2012-06-26T19:47:35.303 回答
0

...愚蠢的我的第二个参数没有路径的默认位置在 C:\program files(x86)\iisexpress

于 2012-06-28T06:54:23.633 回答