chenxin's blog
[原创] core webapi 大文件上传
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
    public class DisableFormValueModelBindingAttribute : Attribute, IResourceFilter
        public void OnResourceExecuting(ResourceExecutingContext context)
            var formValueProviderFactory = context.ValueProviderFactories
            if (formValueProviderFactory != null)
            //下面这段不要忘了,对于 core 6.0及以后版本,网上很多都没有这段。
            var formFileValueProviderFactory = context.ValueProviderFactories
            if (formFileValueProviderFactory != null)

            var jqueryFormValueProviderFactory = context.ValueProviderFactories
            if (jqueryFormValueProviderFactory != null)

        public void OnResourceExecuted(ResourceExecutedContext context)
        [RequestSizeLimit(3L * 1024L * 1024L * 1024L)] //设置允许最大的文件大小为3G
        public async Task<IActionResult> UploadLargeFile([FromServices] IHttpContextAccessor httpContextAccessor)
            var request = httpContextAccessor.HttpContext.Request;

            // validation of Content-Type
            // 1. first, it must be a form-data request
            // 2. a boundary should be found in the Content-Type
            if (!request.HasFormContentType ||
                !MediaTypeHeaderValue.TryParse(request.ContentType, out var mediaTypeHeader) ||
                return new UnsupportedMediaTypeResult();

            var boundary = HeaderUtilities.RemoveQuotes(mediaTypeHeader.Boundary.Value).Value;
            var reader = new MultipartReader(boundary, request.Body);
            var section = await reader.ReadNextSectionAsync();

            // This sample try to get the first file from request and save it
            // Make changes according to your needs in actual use
            while (section != null)
                var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition,
                    out var contentDisposition);

                if (hasContentDispositionHeader && contentDisposition.DispositionType.Equals("form-data") &&
                    // Don't trust any file name, file extension, and file data from the request unless you trust them completely
                    // Otherwise, it is very likely to cause problems such as virus uploading, disk filling, etc
                    // In short, it is necessary to restrict and verify the upload
                    // Here, we just use the temporary folder and a random file name

                    // Get the temporary folder, and combine a random file name with it
                    var fileName = Path.GetRandomFileName();
                    var saveToPath = Path.Combine(Path.GetTempPath(), fileName);

                    using (var targetStream = sysio.File.Create(saveToPath))
                        await section.Body.CopyToAsync(targetStream);

                    return new OkResult();

                section = await reader.ReadNextSectionAsync();

            // If the code runs to this location, it means that no files have been saved
            return new BadRequestObjectResult("No files data in the request.");



非特殊说明,本文版权归 陈新 所有,转载请注明出处.
本文标题 core webapi 大文件上传
{{getUserType(item.userInfo.userType)}} {{formatCommentTime(item.commentDate)}}
{{getUserType(replyItem.userInfo.userType)}} {{formatCommentTime(replyItem.commentDate)}}
正在加载评论列表... 已经到底啦~~~
文章归档 网站地图 闽ICP备2020021271号-1 百度统计