【发布时间】:2017-07-10 13:20:02
【问题描述】:
我尝试将一个基本文件从我的 C# 集成测试项目发送到 web api。 但我不知道为什么,每次调用都会出现异常。
Json.JsonSerializationException:从“System.Io.FileStream”上的“ReadTimeout”获取值时出错
我发现这个属性无法读取,所以也许这就是我的 httpclient 无法序列化它的原因。 那么如何将文件发送到 Web api 呢?
这是我来自客户端的代码:
using (StreamReader reader = File.OpenText("SaveMe.xml"))
{
response = await client.PostAsJsonAsync($"api/registration/test/", reader.BaseStream);
response.EnsureSuccessStatusCode();
}
还有我的控制器:
[Route("api/registration")]
public class RegistrationController : Controller
{
[HttpPost, Route("test/")]
public ActionResult AddDoc(Stream uploadedFile)
{
if (uploadedFile != null)
{
return this.Ok();
}
else
{
return this.NotFound();
}
}
【问题讨论】:
-
我怀疑你不能使用
FileStream。您也可以查看:stackoverflow.com/questions/10320232/how-to-accept-a-file-post -
好的,我今晚会检查一下
-
对不起,但我无法从您的链接中找到一些帮助 :-( 顺便说一下,没有解释如何使用 httpCLient 发送文件。我更新了我的帖子。我的控制器等待[Stream] 而不是 [FileStream]
-
@MehdiBugnard 确认您使用的是哪个版本的 asp.net-mvc。 asp.net-core 还是 asp.net-web-api?
-
在我看来您正在发送 XML 并说它是 JSON。听起来好像不行。
标签: c# .net asp.net-web-api asp.net-web-api2 httpclient