C#FTP上传不太适用于Linux环境

时间:2016-01-25 21:41:54

标签: c# linux ftp mono

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

如果没有页面上包含的所有详细信息,我将如何上传文件?

要上传的文件不包含上传详细信息,正如预期的那样 - 只是明确了。

1 个答案:

答案 0 :(得分:0)

@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