不支持提供的路径格式

问题描述 投票:0回答:2

我正在尝试通过FTP上传文件

不支持所提供的路径格式

代码:

[HttpPost]
    public ActionResult Guardar_registro(Models.CascadingModelLevantamiento model, HttpPostedFileBase file)
    {
      var NombreArchivo = Path.GetFileName(file.FileName);
      string name = Path.Combine("" + NombreArchivo);
      string ftpfullpath = Path.Combine(@"ftp://xxx.xxx.xx.xx:xx" + @"/test/" + name);
      FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
      ftp.Credentials = new NetworkCredential("[email protected]", "xxx");
      ftp.KeepAlive = true;
      ftp.UseBinary = true;
      ftp.Method = WebRequestMethods.Ftp.UploadFile;
      FileStream fs = System.IO.File.OpenRead(ftpfullpath);
      byte[] buffer = new byte[fs.Length];
      fs.Read(buffer, 0, buffer.Length);
      fs.Close();
      Stream ftpstream = ftp.GetRequestStream();
      ftpstream.Write(buffer, 0, buffer.Length);
      ftpstream.Close();
 }
c# model-view-controller ftp
2个回答
0
投票
using (var client = new WebClient()) { client.Credentials = new NetworkCredential(ftpUsername, ftpPassword); client.UploadFile("ftp://host/path.zip", WebRequestMethods.Ftp.UploadFile, localFile); }

资源链接:WebClient


0
投票
类似

string filename = System.IO.Path.Combine(@"C:\local\path", NombreArchivo); FileStream fs = System.IO.File.OpenRead(filename);

尽管在其他答案中正确注释,但更容易的方法是使用WebClient.UploadFile
请参见Upload file and download file from FTP
© www.soinside.com 2019 - 2024. All rights reserved.