C# 使用multipart form-data方式post数据到服务器
作者:三五月儿 时间:2023-12-20 21:10:58
使用multipart/form-data方式提交数据与普通的post方式有一定区别。multipart/form-data的请求头必须包含一个特殊的头信息:Content-Type,其值必须为multipart/form-data。另外还需要规定一个内容分割符用于分割请求体中的多个post的内容,如文件内容和文本内容,只有这样服务端才能正常解析数据。但是,multipart/form-data的基础还是post,它是由post方法来实现的。下面分别给出两种方法提交multipart/form-data数据。
1、使用form表单提交数据
<form action="xx.php" method="post" enctype="multipart/form-data">
<input type="text" name="uname" class="uname" /><br />
<input type="text" name="email" class="email" /><br />
<input type="file" name="file" class="file" /><br />
<input type="submit" name="submit" value="提交"/>
</form>
form表单提交数据的两种方式。
(1)application/x-www-form-urlencoded 不能用于上传文件,只能提交文本,当然如果有file控件的话也只能提交文件名。
(2)multipart/form-data 用于上传文件。
2、使用HttpClient和MultipartFormDataContent
using (var client = new HttpClient())
using (var content = new MultipartFormDataContent())
{
client.BaseAddress = new Uri("http://localhost/WapAPIExp/");
var fileContent1 = new ByteArrayContent(File.ReadAllBytes(@"D:\xx.jpg"));
fileContent1.Headers.ContentDisposition = new ContentDispositionHeaderValue("file")
{
FileName = "xx.jpg"
};
var dataContent = new ByteArrayContent(Encoding.UTF8.GetBytes("1"));
dataContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form")
{
Name = "type"
};
content.Add(fileContent1);
content.Add(dataContent);
var result = client.PostAsync("api/Upload", content).Result;
}
来源:https://blog.csdn.net/yl2isoft/article/details/64482570
标签:multipart,form-data,post
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
IntelliJ IDEA 15款超级牛逼插件推荐(自用,超级牛逼)
2023-10-10 05:51:22
![](https://img.aspxhome.com/file/2023/6/71166_0s.png)
C/C++中typedef的用法大全
2023-02-11 08:57:47
C#入门教程之ListBox控件使用方法
2023-09-20 04:52:59
![](https://img.aspxhome.com/file/2023/3/94583_0s.jpg)
Android RecycleView和线型布局制作聊天布局
2022-08-01 15:36:56
![](https://img.aspxhome.com/file/2023/2/108302_0s.jpg)
完美解决关于禁止ViewPager预加载的相关问题
2021-07-05 11:50:50
IDEA编译报错:Error:java:无效的源发行版:17的解决办法
2023-08-25 10:38:06
![](https://img.aspxhome.com/file/2023/3/62453_0s.png)
C#Process的OutputDataReceived事件不触发问题及解决
2023-08-12 19:22:41
Android横竖屏切换及其对应布局加载问题详解
2023-12-17 20:45:13
C#使用JArray和JObject封装JSON对象
2022-10-15 16:56:16
![](https://img.aspxhome.com/file/2023/8/77238_0s.png)
Java Swing实现窗体添加背景图片的2种方法详解
2021-10-26 19:01:18
![](https://img.aspxhome.com/file/2023/1/62231_0s.jpg)
关于SpringBoot静态资源路径管理问题
2022-12-04 03:51:29
![](https://img.aspxhome.com/file/2023/1/92551_0s.png)
C#中单例模式的三种写法示例
2023-05-04 19:28:25
C# 使用Fluent API 创建自己的DSL(推荐)
2022-03-30 23:36:48
java中staticclass静态类详解
2021-10-12 19:47:35
java中的String定义的字面量最大长度是多少
2023-09-01 08:35:31
![](https://img.aspxhome.com/file/2023/9/61959_0s.png)
Java容器ArrayList知识点总结
2023-09-05 17:46:04
Java多线程编程中使用Condition类操作锁的方法详解
2023-10-19 13:30:55
C#正则表达式匹配HTML中的图片路径,图片地址代码
2023-03-10 16:33:48
C#访问及调用类中私有成员与方法示例代码
2022-06-02 05:30:35
![](https://img.aspxhome.com/file/2023/5/96375_0s.jpg)
JAVA Future类的使用详解
2023-11-04 22:51:20
![](https://img.aspxhome.com/file/2023/5/88515_0s.jpg)