我正在尝试通过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();
}
using (var client = new WebClient())
{
client.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
client.UploadFile("ftp://host/path.zip", WebRequestMethods.Ftp.UploadFile, localFile);
}
资源链接:WebClient
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。