C#使用Multipart form-data方式上传文件及提交其他数据
C#使用Multipart form-data方式上传文件及提交其他数据
扫一扫加微信
C# Code:
/// <summary>
/// C#使用form-data方式上传文件及提交其他数据
/// </summary>
/// <param name="url">接口地址</param>
/// <param name="model">参数模型</param>
/// <returns></returns>
public static string PostFormData(string url, HXQPrivateContractRequest model)
{
//PDF文件路径
string filePath = model.pdfFile;
using (var client = new HttpClient())
{
List<ByteArrayContent> list = new List<ByteArrayContent>();
//title:参数
var dataContent = new ByteArrayContent(Encoding.UTF8.GetBytes(model.title));
dataContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "title"
};
list.Add(dataContent);
//keyword:参数
var dataContent2 = new ByteArrayContent(Encoding.UTF8.GetBytes(model.keyword));
dataContent2.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "keyword"
};
list.Add(dataContent2);
//logUserPhone:参数
var dataContent3 = new ByteArrayContent(Encoding.UTF8.GetBytes(model.logUserPhone));
dataContent3.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "logUserPhone"
};
list.Add(dataContent3);
//处理文件内容
List<ByteArrayContent> list2 = new List<ByteArrayContent>();
byte[] bmpBytes = File.ReadAllBytes(filePath);
var fileContent = new ByteArrayContent(bmpBytes);//填充图片字节
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "files",//Name = "file"
FileName = Path.GetFileName(filePath)
};
list.Add(fileContent);
using (var content = new MultipartFormDataContent())
{
Action<List<ByteArrayContent>> act = (dataContents) =>
{//声明一个委托,该委托的作用就是将ByteArrayContent集合加入到MultipartFormDataContent中
foreach (var byteArrayContent in dataContents)
{
content.Add(byteArrayContent);
}
};
act(list);//执行act
try
{
var result = client.PostAsync(url, content).Result;//post请求
string data = result.Content.ReadAsStringAsync().Result;
return data;//返回操作结果
}
catch (Exception ex)
{
return ex.Message;
}
}
}
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
/// C#使用form-data方式上传文件及提交其他数据
/// </summary>
/// <param name="url">接口地址</param>
/// <param name="model">参数模型</param>
/// <returns></returns>
public static string PostFormData(string url, HXQPrivateContractRequest model)
{
//PDF文件路径
string filePath = model.pdfFile;
using (var client = new HttpClient())
{
List<ByteArrayContent> list = new List<ByteArrayContent>();
//title:参数
var dataContent = new ByteArrayContent(Encoding.UTF8.GetBytes(model.title));
dataContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "title"
};
list.Add(dataContent);
//keyword:参数
var dataContent2 = new ByteArrayContent(Encoding.UTF8.GetBytes(model.keyword));
dataContent2.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "keyword"
};
list.Add(dataContent2);
//logUserPhone:参数
var dataContent3 = new ByteArrayContent(Encoding.UTF8.GetBytes(model.logUserPhone));
dataContent3.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "logUserPhone"
};
list.Add(dataContent3);
//处理文件内容
List<ByteArrayContent> list2 = new List<ByteArrayContent>();
byte[] bmpBytes = File.ReadAllBytes(filePath);
var fileContent = new ByteArrayContent(bmpBytes);//填充图片字节
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "files",//Name = "file"
FileName = Path.GetFileName(filePath)
};
list.Add(fileContent);
using (var content = new MultipartFormDataContent())
{
Action<List<ByteArrayContent>> act = (dataContents) =>
{//声明一个委托,该委托的作用就是将ByteArrayContent集合加入到MultipartFormDataContent中
foreach (var byteArrayContent in dataContents)
{
content.Add(byteArrayContent);
}
};
act(list);//执行act
try
{
var result = client.PostAsync(url, content).Result;//post请求
string data = result.Content.ReadAsStringAsync().Result;
return data;//返回操作结果
}
catch (Exception ex)
{
return ex.Message;
}
}
}
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
C# Code:
public class HXQPrivateContractRequest
{
public string pdfFile { get; set; }
public string title { get; set; }
public string keyword { get; set; }
public string logUserPhone { get; set; }
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
{
public string pdfFile { get; set; }
public string title { get; set; }
public string keyword { get; set; }
public string logUserPhone { get; set; }
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
扫一扫加微信
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网