【问题标题】:C# FTP upload not quite working in Linux environmentC# FTP 上传在 Linux 环境中不太工作
【发布时间】:2016-01-25 21:41:53
【问题描述】:

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

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

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

【问题讨论】:

  • Somhow 单声道实现如何将 HTTP 协议相关的东西放入这个上传请求中,就好像你在传输一些表单数据一样,很奇怪。您是否尝试过使用FtpWebRequest 类(尤其是设置为WebRequestMethods.Ftp.UploadFile (msdn.microsoft.com/en/library/…) 的方法,例如github.com/mono/gert/blob/master/standalone/bug478451/test.cs 中使用的样式?
  • @MaximilianGerhardt 现在完美运行,非常感谢
  • 您可以发布您的工作代码并接受它作为答案,以便其他人可以从这个问题中受益:)

标签: c# linux ftp mono


【解决方案1】:

@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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-05
    • 2015-01-09
    相关资源
    最近更新 更多