您的位置:首页 > 科技 > IT业 > 济南百度爱采购_邹城网站定制_seo在线短视频发布页_河北seo推广

济南百度爱采购_邹城网站定制_seo在线短视频发布页_河北seo推广

2024/12/23 16:14:35 来源:https://blog.csdn.net/Hui_Hong_TaiLang/article/details/144549387  浏览:    关键词:济南百度爱采购_邹城网站定制_seo在线短视频发布页_河北seo推广
济南百度爱采购_邹城网站定制_seo在线短视频发布页_河北seo推广

在这里插入图片描述

请求发起

.NET Framework 3.5 版

/// <summary>/// 使用multipart/form-data方式上传文件及其他数据/// </summary>/// <param name="headers">请求头参数</param>/// <param name="nameValueCollection">键值对参数</param>/// <param name="fileCollection">文件参数:参数名,文件路径</param>/// <returns>接口返回结果</returns>public static string PostMultipartFormData(string url, Dictionary<string, string> headers, NameValueCollection nameValueCollection, NameValueCollection fileCollection){using (var client = new HttpClient()){foreach (var item in headers){client.DefaultRequestHeaders.Add(item.Key, item.Value);}using (var content = new MultipartFormDataContent()){// 键值对参数string[] allKeys = nameValueCollection.AllKeys;foreach (string key in allKeys){var dataContent = new ByteArrayContent(Encoding.UTF8.GetBytes(nameValueCollection[key]));dataContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data"){Name = key};content.Add(dataContent);}//处理文件内容string[] fileKeys = fileCollection.AllKeys;foreach (string key in fileKeys){byte[] bmpBytes = File.ReadAllBytes(fileCollection[key]);var fileContent = new ByteArrayContent(bmpBytes);//填充文件字节fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data"){Name = key,FileName = Path.GetFileName(fileCollection[key])};content.Add(fileContent);}var result = client.PostAsync(url, content).Result;//post请求string data = result.Content.ReadAsStringAsync().Result;return data;//返回操作结果}}}

.NET Framework 4.+ 版

/// <summary>/// 使用multipart/form-data方式上传文件及其他数据/// </summary>/// <param name="headers">请求头参数</param>/// <param name="nameValueCollection">键值对参数</param>/// <param name="fileCollection">文件参数:参数名,文件路径</param>/// <returns>接口返回结果</returns>public static string PostMultipartFormData(string url, Dictionary<string, string> headers, NameValueCollection nameValueCollection, NameValueCollection fileCollection){using (var client = new HttpClient()){foreach (var item in headers){client.DefaultRequestHeaders.Add(item.Key, item.Value);}using (var content = new MultipartFormDataContent()){// 键值对参数string[] allKeys = nameValueCollection.AllKeys;foreach (string key in allKeys){var dataContent = new ByteArrayContent(Encoding.UTF8.GetBytes(nameValueCollection[key]));dataContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data"){Name = key};content.Add(dataContent);}//处理文件内容string[] fileKeys = fileCollection.AllKeys;foreach (string key in fileKeys){byte[] bmpBytes = File.ReadAllBytes(fileCollection[key]);var fileContent = new ByteArrayContent(bmpBytes);//填充文件字节fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data"){Name = key,FileName = Path.GetFileName(fileCollection[key])};content.Add(fileContent);}var result = client.PostAsync(url, content).Result;//post请求string data = result.Content.ReadAsStringAsync().Result;return data;//返回操作结果}}}

Web API 接收接口
ASP .NET Core Web API 接口接收 multipart/form-data 文件、数据

[HttpPost]//public string Post([FromForm] string directory, [FromForm] IList<IFormFile> files )public string Post([FromForm] string directory, [FromForm] IFormFile files){return "";}[HttpPut]public string Put([FromForm] IFormFile templateFile, [FromForm] string id, [FromForm] string objectVersionNumber){return "";}

ASP.NET 4.x 版

[HttpPost(Name = "PostWeatherForecast")]//public string Post([FromForm] string directory, [FromForm] IList<IFormFile> files )public string Post([FromForm] string directory, [FromForm] IFormFile files){// Check if the request contains multipart/form-data.if (!Request.ContentType.IsMimeMultipartContent()){throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);}string root = Request.HttpContext.Current.Server.MapPath("~/App_Data");var provider = new MultipartFormDataStreamProvider(root);try{// Read the form data.await Request.Content.ReadAsMultipartAsync(provider);// This illustrates how to get the file names.//foreach (MultipartFileData file in provider.FileData)//{//    Trace.WriteLine(file.Headers.ContentDisposition.FileName);//    Trace.WriteLine("Server file path: " + file.LocalFileName);//}//  return Request.CreateResponse(HttpStatusCode.OK);}catch (System.Exception e){//  return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);}return "";}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com