【问题标题】:Trying to retrieve a file from another computer on same network尝试从同一网络上的另一台计算机检索文件
【发布时间】:2019-12-10 12:23:18
【问题描述】:

我正在尝试从与我位于同一网络的计算机上复制图像文件,但遇到了异常。

我在 Raspberry Pi 上作为 aspnet 核心应用程序运行。带有图像的计算机是同一网络上的 android 设备。 要复制的图片地址为:ftp://172.18.10.190:8010/records/2019-07-26/157395.jpg 我可以将该链接放入浏览器并检索图像,但我想将其保存到 Pi。

我尝试过使用 File.Copy:

System.IO.File.Copy("ftp://172.18.10.190:8010/records/2019-07-26/157395.jpg", directory + "/" + "157395.jpg");

我收到未找到目录的错误: NotFoundException:找不到路径“home/pi/pcmonitor/ftp:/172.18.10.190:8010/records/2019-07-26/157395.jpg”的一部分

在删除 ftp 后,'home/pi/pcmonitor/' 似乎与正斜杠之一一起被前置。

【问题讨论】:

    标签: file asp.net-core lan


    【解决方案1】:

    您无法通过File.Copy 复制或下载 ftp 文件。

    您需要发送请求并保存响应,如

    // Get the object used to communicate with the server.
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpPath);
    request.Method = WebRequestMethods.Ftp.DownloadFile;
    
    // This example assumes the FTP site uses anonymous logon.
    request.Credentials = new NetworkCredential("xx", "xx");
    
    FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    
    Stream responseStream = response.GetResponseStream();
    using (var fileStream = File.Create(localPath))
    {
        responseStream.CopyTo(fileStream);
    }
    
    Console.WriteLine($"Download Complete, status {response.StatusDescription}");
    
    response.Close();
    

    【讨论】:

      猜你喜欢
      • 2017-09-22
      • 1970-01-01
      • 2017-12-29
      • 2018-06-15
      • 2016-09-19
      • 1970-01-01
      • 1970-01-01
      • 2020-03-20
      • 2012-03-29
      相关资源
      最近更新 更多