1.图片上传至控制器,本过程有:构建http请求,发送参数以及图片信息
代码为:
#region 存储图片
//开启http访问链接
using (HttpClient client = new HttpClient())
{
//设定要响应的数据格式
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("text/json"));
//定义内容
using (var content = new MultipartFormDataContent())
{
//基于字节数组提供http内容
List<ByteArrayContent> listPara = new List<ByteArrayContent>();
//请求头部
var dataContent = new ByteArrayContent(Encoding.UTF8.GetBytes(saveurl));
dataContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
{
Name = "ImgPath"
};
listPara.Add(dataContent);
//请求数据
List<ByteArrayContent> listData = new List<ByteArrayContent>();
Stream stream = file.InputStream;
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
stream.Seek(0, SeekOrigin.Begin);
//数据部分
ByteArrayContent fileContemt = new ByteArrayContent(bytes);
//定义文件名
fileContemt.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
{
FileName = fileNameNew
};
listData.Add(fileContemt);
//申明一个方法 将ByteArray