当前位置:网站首页>ASP.Net Core创建MVC项目上传多个文件(流方式)
ASP.Net Core创建MVC项目上传多个文件(流方式)
2022-07-22 21:20:00 【gc_2299】
将之前文章《ASP.Net Core创建MVC项目上传单个文件(流方式)》的前端稍微调整一下,即可支持上传多个文件,修改的内容主要是在input的标签中增加multiple属性。程序可以正常将多个文件上传到指定文件夹,如下图所示:

但当上传文件较大时,就无法上传,测试之后发现,几M、十几M的文件可以传上去,30M以上的测试了几个都无法上传。
通过查阅微软帮助文档,其中介绍了Kestrel 最大请求正文大小其默认的最大请求正文大小为 30,000,000 个字节,约为 28.6 MB,这也和上面测试的结果相符,为了增加最大上传文件大小,可以通过以下几种方式配置(以下内容来自微软帮助文档)。
1)program.cs中配置,配置代码如下所示:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureKestrel((context, options) =>
{
// Handle requests up to 50 MB
options.Limits.MaxRequestBodySize = 52428800;
})
.UseStartup<Startup>();
});
2)MVC项目的话,可以在页面处理程序类或操作方法中配置,如下所示:
// Handle requests up to 50 MB
[RequestSizeLimit(52428800)]
public class BufferedSingleFileUploadPhysicalModel : PageModel
{
...
}
3)Razor Pages项目的话,可以在Startup.ConfigureServices 中通过约定应用筛选器:
services.AddRazorPages(options =>
{
options.Conventions
.AddPageApplicationModelConvention("/FileUploadPage",
model =>
{
// Handle requests up to 50 MB
model.Filters.Add(
new RequestSizeLimitAttribute(52428800));
});
});
本文中采用第二种方式,在上传函数中增加特性以配置上传内容大小,代码如下所示:
[HttpPost]
[RequestSizeLimit(104837600)]
public async Task<IActionResult> UploadPhysical()
{
...
}
按上述配置后,即可上传大型文件,不过这里限制的是总的内容大小,即所有上传文件大小之和不能超过设置的大小,超过则无法上传。
测试过程中还是解决不了显示文件列表和显示上传进度的问题,后续会开始学习前端ui框架,采用UI框架中提供的文件上传组件和功能,结合ASP.Net Core实现文件上传。
参考文献
[1]https://docs.microsoft.com/zh-cn/aspnet/core/mvc/models/file-uploads?view=aspnetcore-6.0
边栏推荐
- Small program completion work wechat campus second-hand book trading small program graduation design finished product (2) small program function
- Redis——JedisConnectionException Could not get a resource from the pool
- Overview of multisensor fusion -- FOV and bev
- Application of the latest version of Ontrack easyrecovery computer data recovery software
- Wechat campus second-hand book trading applet graduation design finished product (6) opening defense ppt
- Wechat campus second-hand book trading applet graduation design finished product (5) assignment
- Utools recommendation
- Uniapp switches the tab bar to display different pages, remembers the page location and pulls up to get new data
- Copytexture, copytoresolvetarget of UE4 engine
- 一文深入浅出理解国产开源木兰许可系列协议
猜你喜欢

Information system project managers must recite the core examination points (49) contract law

一个浏览器用户访问服务器文件的WEB服务器

Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE)

Basic commands of redis' five basic data types

ETL tool (data synchronization)

LeetCode(剑指 Offer)- 11. 旋转数组的最小数字

妙用cURL

JS determines the scrolling element and solves the tab to switch the scrolling position independently

GNU LD script command language (I)

Kubernetes 部署策略
随机推荐
93.(leaflet篇)leaflet态势标绘-进攻方向修改
百度搜索打击盗版网文站点:互联网内容侵权现象为何屡禁不止
多传感器融合综述---FOV与BEV
2022 employment season surprise! The genuine Adobe software can finally be used for nothing
《postgresql指南--内幕探索》第三章查询处理
Information system project managers must recite the core examination points (49) contract law
打板遇到的问题
Educational Codeforces Round 132 A - D
《postgresql指南--内幕探索》第一章 数据库集簇、数据库和数据表
Is cross modal semantic alignment optimal under comparative learning--- Adaptive sparse attention alignment mechanism IEEE trans MultiMedia
Wechat campus second-hand book trading applet graduation design finished product (1) development outline
2022就业季惊喜来袭!正版Adobe软件,终于能正经白嫖一把了
Ftxui basic notes (Hello World)
Uniapp switches the tab bar to display different pages, remembers the page location and pulls up to get new data
使用Flutter与贝塞尔曲线画一个波浪球
聊聊并发编程的12种业务场景
Classes and objects (1)
FTXUI基础笔记(hello world)
Leetcode 757 set the intersection size to at least 2[sort greedy] the leetcode path of heroding
Small program completion work wechat campus second-hand book trading small program graduation design finished product (2) small program function