【问题标题】:File Uploading using HttpClient in .Net core 3.0 Console Application在 .Net core 3.0 控制台应用程序中使用 HttpClient 上传文件
【发布时间】:2021-01-05 19:33:23
【问题描述】:

我正在尝试在 Asp.net Core 3 中使用 HttpClient 上传文件,但它没有将文件上传到服务器。如果我尝试通过 Postman 将文件上传到服务器,它可以工作。

任何帮助将不胜感激。

下面是我上传文件的简单代码。

HttpClient _client = new HttpClient();
var stream = new FileStream("main.txt", FileMode.Open);
byte[] fileBytes = new byte[stream.Length];
stream.Write(fileBytes, 0, (int)stream.Length);
stream.Dispose();

using (var content = new MultipartFormDataContent())
{
    var fileContent = new ByteArrayContent(fileBytes);
    fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
    {
        FileName = "Test",
    };
    fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");
    content.Add(fileContent);

    _client.PostAsync("http://192.168.56.1:8000", content);                
}

正如我上面所说,它正在与邮递员合作。我正在放一张截图,显示我如何使用邮递员。

当我调试代码时,我得到以下错误。

【问题讨论】:

  • 谁能帮我解决这个问题?

标签: asp.net-core-3.0 .net-core-3.0


【解决方案1】:

一种解决方案是您可以使用MemoryStream 来转换文件的内容。您的方法会导致 main.txt 文件中的内容为空。

像这样更改您的代码:

HttpClient _client = new HttpClient();
Stream stream = new FileStream("main.txt", FileMode.Open);​ 
MemoryStream ms = new MemoryStream();​
stream.CopyTo(ms);​
byte[] fileBytes = ms.ToArray();​
ms.Dispose(); ​

另一种方法是使用System.IO.File.ReadAllBytes(filePath)

尝试使用下面的示例代码发布文件,请参阅我的answer

using (var client = new HttpClient())
        {
            using (var content = new MultipartFormDataContent())
            {

                //replace with your own file path, below use an txt in wwwroot for example
                string filePath = Path.Combine(_hostingEnvironment.WebRootPath, "main.txt");

                byte[] file = System.IO.File.ReadAllBytes(filePath);

                var byteArrayContent = new ByteArrayContent(file);

                content.Add(byteArrayContent, "file", "main.txt");

                var url = "https://localhost:5001/foo/bar";
                var result = await client.PostAsync(url, content);

            }
        }

foo/bar 动作

[HttpPost]
[Route("foo/bar")]
public IActionResult ProcessData([FromForm]IFormFile file)
{
     //your logic to upload file
}

【讨论】:

  • 感谢 Xing Zou,我尝试了您的示例,但出现了我在问题中提到的相同错误。基本上我有一个控制台应用程序正在尝试将文件上传到服务器。
  • 我从网上下载了miniweb http服务器,只是为了通过url从本地驱动器下载和上传文件。我已经从这里下载了sourceforge.net/projects/miniweb
  • @use3661407 我不知道。也许你需要检查你的服务器日志。
  • 没有任何服务器日志。我刚刚从互联网上下载了服务器,它是一个 exe 文件。我看不到任何日志。
【解决方案2】:

我已经下载了服务器并使用此代码对其进行了测试。服务器返回 200 OK

using (var client = new HttpClient())
{
   using (var content = new MultipartFormDataContent())
   {
      using (var fileStream = new FileStream("test.txt", FileMode.Open))
      {
         var fileContent = new StreamContent(fileStream);

          content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");
          content.Add(fileContent, "file", "test.txt");

          var response = await client.PostAsync("http://192.168.56.1:8000/", content);
       }
   }
}

【讨论】:

  • 感谢您的快速回复。我之前尝试过等待,但什么也没发生。我无法找到我缺少某些东西的地方。我在谷歌上搜索了每个示例都展示了相同的实现,这是我在我的问题中提出的。
  • @user3661407,增加了一个想法
  • 您好用 Postman 上传文件吗?可以附上截图吗?
  • Anthon,我刚刚用邮递员屏幕截图更新了这个问题。关于错误-我没有收到任何错误。一切正常。
  • 请尝试获取请求的响应并查看状态码和消息var response = await _client.PostAsync(..);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多