我在通过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

关于c# - C#FTP上传在Linux环境中无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35002934/

10-09 08:43