.Net Core文件上传
https://www.cnblogs.com/viter/p/10074766.html
1.内置了很多种绑定模型 缺少了一个FromFileAttribute 绑定模型 需要自己实现一个
public class FromFileAttribute : Attribute, IBindingSourceMetadata
{
public BindingSource BindingSource => BindingSource.FormFile;
}
2.实现一个文件上传类
public class UserFile
{
public string FileName { get; set; }
public long Length { get; set; }
public string Extension { get; set; }
public string FileType { get; set; } private readonly static string[] Filters = { ".jpg", ".png", ".bmp" };
public bool IsValid => !string.IsNullOrEmpty(this.Extension) && Filters.Contains(this.Extension); private IFormFile file;
public IFormFile File
{
get { return file; }
set
{
if (value != null)
{
this.file = value; this.FileType = this.file.ContentType;
this.Length = this.file.Length;
this.Extension = this.file.FileName.Substring(file.FileName.LastIndexOf('.'));
if (string.IsNullOrEmpty(this.FileName))
this.FileName = this.FileName;
}
}
} public async Task<string> SaveAs(string destinationDir = null)
{
if (this.file == null)
throw new ArgumentNullException("没有需要保存的文件"); if (destinationDir != null)
//如果路径不存在 则创建路径(包括子路径)
Directory.CreateDirectory(destinationDir);
var newName = DateTime.Now.Ticks;
//定义文件名
var newFile = Path.Combine(destinationDir ?? "", $"{newName}{this.Extension}");
//建立一个文件缓冲流 也就是开辟一块内存空间
using (FileStream fs = new FileStream(newFile, FileMode.CreateNew))
{
//将上传的文件拷贝到目标流中
await this.file.CopyToAsync(fs);
//清除文件缓冲区 并将所有缓冲数据写入到文件中
fs.Flush();
}
return newFile;
}
}
3.实现上传接口
[HttpPost]
public async Task<IActionResult> JcbPost([FromFile]UserFile file)
{
if (file == null || !file.IsValid)
return new JsonResult(new { code = , message = "不允许上传的文件类型" });
//获取当前程序运行的根目录 可以不需要
var directory = System.IO.Directory.GetCurrentDirectory();
string newFile = string.Empty;
if (file != null)
//在Web.Host根路径下(相对路径)
newFile = await file.SaveAs("data/files/images"); return new JsonResult(new { code = , message = "成功", url = newFile });
}
.Net Core文件上传的更多相关文章
- [转载]ASP.NET Core文件上传与下载(多种上传方式)
ASP.NET Core文件上传与下载(多种上传方式) 前言 前段时间项目上线,实在太忙,最近终于开始可以研究研究ASP.NET Core了. 打算写个系列,但是还没想好目录,今天先来一篇,后面在 ...
- ASP.NET Core文件上传IFormFile于Request.Body的羁绊
前言 在上篇文章深入探究ASP.NET Core读取Request.Body的正确方式中我们探讨了很多人在日常开发中经常遇到的也是最基础的问题,那就是关于Request.Body的读取方式问题,看是简 ...
- ASP.NET Core 文件上传
前言 上篇博文介绍了怎么样在 asp.net core 使用 Redis 和 Protobuf 进行 Session缓存.本篇的是开发过程中使用的一个小功能,怎么做单文件和多文件上传. 如果你觉得对你 ...
- ASP.NET Core文件上传与下载(多种上传方式)
前言 前段时间项目上线,实在太忙,最近终于开始可以研究研究ASP.NET Core了. 打算写个系列,但是还没想好目录,今天先来一篇,后面在整理吧. ASP.NET Core 2.0 发展到现在,已经 ...
- Asp.Net Core 文件上传处理
本文主要介绍后台接收处理 1.在使用控制器接收 : [HttpPost] : public IActionResult UploadFiles(IList<IFormFile> files ...
- Asp.Net Core文件上传
文件上传功能在实际开发中经常使用,在 .Net Core中,文件上传接收类型不再使用 HttpPostedFile 或 HttpFileCollection来接收,而是使用 IFormFile 或 I ...
- ASP.NET Core文件上传、下载与删除
首先我们需要创建一个form表单如下: <form method="post" enctype="multipart/form-data" asp-con ...
- 解决ASP.NET Core Mvc文件上传限制问题
一.简介 在ASP.NET Core MVC中,文件上传的最大上传文件默认为20MB,如果我们想上传一些比较大的文件,就不知道怎么去设置了,没有了Web.Config我们应该如何下手呢? 二.设置上传 ...
- .net core版 文件上传/ 支持批量上传,拖拽以及预览,bootstrap fileinput上传文件
asp.net mvc请移步 mvc文件上传支持批量上传,拖拽以及预览,文件内容校验 本篇内容主要解决.net core中文件上传的问题 开发环境:ubuntu+vscode 1.导入所需要的包:n ...
随机推荐
- Python selenium巧用Javascript脚本注入解决按钮点选问题
前段时间,笔者忙于应付公司组织的雅思考试,白天.晚上但凡有空,笔者都是埋头伏案,啃剑桥雅思(剑4~剑12)的官方模拟题或者做着与雅思考试相关的准备工作,这个过程持续了40余天.最近总算鼓起勇气走进考场 ...
- IO多路复用之select,poll,epoll个人理解
在看这三个东西之前,先从宏观的角度去看一下,他们的上一个范畴(阻塞IO和非阻塞IO和IO多路复用) 阻塞IO:套接口阻塞(connect的过程是阻塞的).套接口都是阻塞的. 应用程序进程-----re ...
- Ubuntu设置su和sudo为不需要密码 (摘录自别处)
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/langb2014/article/details/54646675 针对非服务器用户,只是用普通的U ...
- 关于JS的原型与继承笔记
1.什么是原型? 原型就是公用的方法或者属性. 1.prototype本质上还是一个JavaScript对象: 2.每个函数都有一个默认的属性prototype,而这个prototype的constr ...
- 23.C# 语言的改进
1.对象初始化器 class Curry { public string MainIngredient{get;set;} public string Style { get; set; } publ ...
- VMware密匙
5A02H-AU243-TZJ49-GTC7K-3C61N 新版本密室:FF31K-AHZD1-H8ETZ-8WWEZ-WUUVA
- Java -- 基于JDK1.8的ArrayList源码分析
1,前言 很久没有写博客了,很想念大家,18年都快过完了,才开始写第一篇,争取后面每周写点,权当是记录,因为最近在看JDK的Collection,而且ArrayList源码这一块也经常被面试官问道,所 ...
- rpgmakermv \c 常用颜色一览
1 2 3 4 5 6 7 14 18
- Install rapyuta client on Ubuntu14.04
# -Rapyuta-installation-in-Ubuntu14.04-LTS-Trusty-This gzip folder is a tested version which can ins ...
- zw版足彩大数据&报价
zw版足彩大数据&报价 ::zw增强版足彩大数据,文件名后缀是'.dat' ::文件格式是标准文本格式,逗号分隔 ::zw增强版,在标准版赔率基础上,增加了倒数.比率两组归一化数据 ::zw版 ...