不支持提供的路径格式
The provided format of the path is not supported
我正在尝试通过 FTP
上传文件
The provided format of the path is not supported
代码:
[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("xxx@xx.com", "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);
}
资源link:WebClient
到达 File.OpenRead
的内容必须是有效的本地 (Windows) 路径。不是 FTP URL.
类似于
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。
我正在尝试通过 FTP
上传文件The provided format of the path is not supported
代码:
[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("xxx@xx.com", "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);
}
资源link:WebClient
到达 File.OpenRead
的内容必须是有效的本地 (Windows) 路径。不是 FTP URL.
类似于
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。