Upload file uploaded via HTTP to ASP.NET further to FTP server in C#(将通过 HTTP 上传到 ASP.NET 的文件上传到 C# 中的 FTP 服务器)
问题描述
上传表格:
<form asp-action="Upload" asp-controller="Uploads" enctype="multipart/form-data">
<input type="file" name="file" maxlength="64" />
<button type="submit">Upload</button>
控制器/文件上传:
public void Upload(IFormFile file){
using (WebClient client = new WebClient())
{
client.Credentials = new NetworkCredential("xxxx", "xxxx");
client.UploadFile("ftp://xxxx.xxxx.net.uk/web/wwwroot/images/", "STOR", file.FileName);
}
}
问题:
出现错误找不到文件 xxxx".我知道问题是它试图找到文件,因为它是 FTP 服务器上的 "C:path-to-vs-filesexamplePhoto.jpg"
,这显然不存在.我一直在这里查看许多问题/答案,我认为我需要某种 FileStream
读/写代码.但我目前还没有完全理解这个过程.
Getting error "Could not find file xxxx". I understand the issue is that it's trying to find the file as it is "C:path-to-vs-filesexamplePhoto.jpg"
on the FTP server, which obviously doesn't exist. I've been looking at many questions/answers on here and I think I need some kind of FileStream
read/write cod. But I'm not fully understanding the process at the moment.
推荐答案
使用 IFormFile.CopyTo
或 IFormFile.OpenReadStream
来访问上传文件的内容.
Use IFormFile.CopyTo
or IFormFile.OpenReadStream
to access the contents of the uploaded file.
虽然 WebClient
无法使用 Stream
接口.所以你最好使用 FtpWebRequest
一个>:
Though WebClient
cannot work with Stream
interface. So you better use FtpWebRequest
:
public void Upload(IFormFile file)
{
FtpWebRequest request =
(FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.UploadFile;
using (Stream ftpStream = request.GetRequestStream())
{
file.CopyTo(ftpStream);
}
}
这篇关于将通过 HTTP 上传到 ASP.NET 的文件上传到 C# 中的 FTP 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将通过 HTTP 上传到 ASP.NET 的文件上传到 C# 中的


基础教程推荐
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- 将数据集转换为列表 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 如果条件可以为空 2022-01-01