文件上传和表单字段混合提交

Insus.NET根据实际需求,在用户填写表单和上传文档一次性处理,即是说文件上传和表单字段混合提交,在内存中处理上传的内容。

参考MultipartFormDataStreamProviderhttps://learn.microsoft.com/en-us/previous-versions/aspnet/hh835949(v=vs.118) 【此链接不再定期更新内容】
2026-09-09_06-04-48

 
Web API File Upload, Single or Multiple files https://damienbod.com/2014/03/28/web-api-file-upload-single-or-multiple-files/

 
还可以参考相关详细:
https://github.com/aspnet/AspNetWebStack/tree/main/src/System.Net.Http.Formatting

 

从上面知识点,创建适合自己使用的类,
2026-09-10_05-49-15

 

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Web;

/// <summary>
/// InsusNetMultipartFormDataStreamProvider 的摘要说明
/// </summary>
namespace Insus.NET
{
    public class InsusNetMultipartFormDataStreamProvider : MultipartStreamProvider
    {
        private NameValueCollection _formData = new NameValueCollection();

        private List<HttpContent> _fileContents = new List<HttpContent>();

        private Collection<bool> _isFormData = new Collection<bool>();

        public NameValueCollection FormData
        {
            get { return _formData; }
        }

        public List<HttpContent> Files
        {
            get { return _fileContents; }
        }

        public override Stream GetStream(HttpContent parent, HttpContentHeaders headers)
        {
            ContentDispositionHeaderValue contentDisposition = headers.ContentDisposition;
            if (contentDisposition != null)
            {
                _isFormData.Add(String.IsNullOrEmpty(contentDisposition.FileName));

                return new MemoryStream();
            }

            throw new InvalidOperationException(string.Format("Did not find required '{0}' header field in MIME multipart body part..", "Content-Disposition"));
        }

        public override async Task ExecutePostProcessingAsync()
        {
            for (int index = 0; index < Contents.Count; index++)
            {
                if (_isFormData[index])
                {
                    HttpContent formContent = Contents[index];
                    ContentDispositionHeaderValue contentDisposition = formContent.Headers.ContentDisposition;

                    string formFieldName = UnquoteToken(contentDisposition.Name) ?? String.Empty;

                    string formFieldValue = await formContent.ReadAsStringAsync();


                    FormData.Add(formFieldName, formFieldValue);
                }
                else
                {
                    _fileContents.Add(Contents[index]);
                }
            }
        }

        private static string UnquoteToken(string token)
        {
            if (String.IsNullOrWhiteSpace(token))
            {
                return token;
            }

            if (token.StartsWith("\"", StringComparison.Ordinal) && token.EndsWith("\"", StringComparison.Ordinal) && token.Length > 1)
            {
                return token.Substring(1, token.Length - 2);
            }

            return token;
        }
    }
}
View Code

 

后续博文中,会有相关实例分享...

 

posted @ 2026-09-10 06:18  Insus.NET  阅读(5)  评论(0)    收藏  举报