.NET - 使用HttpClient的以发布文件? [英] .Net - Use HttpClient to POST a file?

查看:262
本文介绍了.NET - 使用HttpClient的以发布文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想用就用多个文件发布到本地端点,以模拟一系列上传的调试方法。

I have a debug method that I'm trying to use to use to post multiple files to a local endpoint to simulate a series of uploads.

我有以下的code:

var fi = new FIleInfo(....);
var form = new MultipartFormDataContent();
form.Add(new StreamContent(fi.OpenRead()), "file", fi.Name);
client.PostAsync(@"http://localhost:12372/TemplateManagement/Asset/Create", form);

这是我要发布的方法具有以下签名(资产来自一个定制绑定,但是这并不重要):

that I want to post to a method with the following signature (asset comes from a custom binder but that's not important):

public JsonResult Create(HttpPostedFileBase file, DynamicBuilderAsset asset)

该职位被提出确定,但该文件参数为空。缺少什么我在这里?

The post gets made ok but the file parameter is null. What am I missing here?

推荐答案

尝试引用的名称和文件名:

Try quoting the name and the filename:

form.Add(new StreamContent(fi.OpenRead()), "\"file\"", "\"" + fi.Name + "\"");

如果你要发送的标准键也是如此:

And the same is true if you are sending standard keys:

form.Add(new StringContent("some asset data"), "\"asset\"");

这篇关于.NET - 使用HttpClient的以发布文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆