C# FTP 上传在 Linux 环境下不太有效

标签 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) 计算机上运行它,我得到 (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/

相关文章:

c# - 发布到我的服务器时外部 javascript 文件未加载

python - 如何杀死由python中的子进程创建的进程?

php - 对某些 CMS 或框架的多个实例使用符号链接(symbolic link)

linux - 当一个分机适用于 Asterisk 中的两个 voIp 时如何给出错误

java - 如何强制 socket.close 释放写 block

csv - 从 FTP 文件夹获取最新更新的文件

c# - 了解 'The SqlParameter is already contained by another SqlParameterCollection'

c# - 确定哪个列表框的事件已经启动

c# - 检查文本框不为空

java - ftp 从 windows 发送到 linux 显示 java.net.ConnectException