zoukankan      html  css  js  c++  java
  • asp.net core 3.1 MultipartFormDataContent multipart/form-data MIME type.

    1.多文件上传,也可以继续添加其他参数,只是参数内容要转为byte[]因为使用ByteArrayContent 他的入参是byte[]

     public async Task<FileInfoUploadResponse> Upload(SourceMaterialParamDto source)
            {
                var chatbotId = WebUtility.UrlEncode(source.ChatbotId);
                var url = string.Format(_massp.MateriaUploadUrl, _massp.Host, _massp.Version, chatbotId);
                using (var message = new MultipartFormDataContent())
                {
                    foreach (var item in source.FormFiles)
                    {
                        var fileContent = new ByteArrayContent(item.Data);
                        if (!new FileExtensionContentTypeProvider().Mappings.TryGetValue(item.Extension, out var contenttype)) throw new Exception("文件格式不存在");//item.Extension=".png" item.FileName="text.png"
                        fileContent.Headers.ContentType = new MediaTypeHeaderValue(contenttype);
                        //fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue($"form-data;name="{item.FileName}";filename="{item.FileName}"");
                        fileContent.Headers.Add("Content-Disposition", $"form-data; name="{item.FileName}"; filename="{item.FileName}"");
                        message.Add(fileContent, item.FileName, item.FileName);
                    }
                    var date = DateTimeOffset.Now;
                    var time = date.ToString("r");
                    string token = CreateAuthorizationToken(time);
                    message.Headers.Add("UploadMode", source.UploadMode);
                    _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("ctToken", token);
                    _httpClient.DefaultRequestHeaders.Date = date;
                    _httpClient.DefaultRequestHeaders.Host = _massp.AppIp;
                    var response = await _httpClient.PostAsync(url, message);
                    if (response.StatusCode != HttpStatusCode.OK) return new FileInfoUploadResponse() { ErrorCode = (int)response.StatusCode, ErrorMessage = response.StatusCode.ToString() };
                    var rlt = await response.Content.ReadAsStringAsync();
                    return JsonConvert.DeserializeObject<FileInfoUploadResponse>(rlt);
                }
            }
  • 相关阅读:
    《面试系列一》一个怂到极点的开始
    NuGet 私房服务使用帮助
    Nuspec 范本文件
    送给前线码农的话 – 大牛们的经典语录
    Visual Studio 增加源码头注释
    为 Orchard 增加 MVC 的脚手架.
    为 Orchard 增加扩展的存储位置
    Orchard 瘦身优化
    Hybrid Multiple Attention Network for Semantic Segmentation in Aerial Images
    【nvidia jetson xavier】 Linux系统安装+Deepstream 5.1环境部署
  • 原文地址:https://www.cnblogs.com/rengke2002/p/13086466.html
Copyright © 2011-2022 走看看