我正在尝试使用C#通过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