我在通过 FTP 上传 JSON 文件时遇到问题
using (WebClient client = new WebClient())
{
client.Credentials = new NetworkCredential("username", "password");
client.UploadFile("ftp://domain.com/file.json", "STOR", "file.json");
}
在我的 (windows) 计算机上运行它,我得到 (desired) 的输出
{
{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
关于C# FTP 上传在 Linux 环境下不太有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35002934/