C# FTP上传在Linux环境中不太有效
本文关键字:有效 环境 Linux FTP | 更新日期: 2023-09-27 18:32:01
我在通过FTP上传JSON文件时遇到问题
using (WebClient client = new WebClient())
{
client.Credentials = new NetworkCredential("username", "password");
client.UploadFile("ftp://domain.com/file.json", "STOR", "file.json");
}
在我的(Windows)计算机上运行它,我得到的输出(所需)
{
{JSON DATA}
}
但是在我的 Ubuntu 服务器上,我得到的输出(不需要)
--------------8d325d822338686
Content-Disposition: form-data; name="file"; filename="file.json"
Content-Type: application/octet-stream
{
{JSON DATA}
}
--------------8d325d822338686--
如何在没有页面上包含所有详细信息的情况下上传文件?
正如预期的那样,要上传的文件不包含上传详细信息 - 只是明确说明。
建议@MaximilianGerhardt发布我的答案
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://domain.com/outputlocationfile.json");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("username", "password");
StreamReader sourceStream = new StreamReader("localfile.json");
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
https://msdn.microsoft.com/en-us/library/ms229715(v=vs.110).aspx