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文件上传的更多相关文章

  1. [转载]ASP.NET Core文件上传与下载(多种上传方式)

    ASP.NET Core文件上传与下载(多种上传方式)   前言 前段时间项目上线,实在太忙,最近终于开始可以研究研究ASP.NET Core了. 打算写个系列,但是还没想好目录,今天先来一篇,后面在 ...

  2. ASP.NET Core文件上传IFormFile于Request.Body的羁绊

    前言 在上篇文章深入探究ASP.NET Core读取Request.Body的正确方式中我们探讨了很多人在日常开发中经常遇到的也是最基础的问题,那就是关于Request.Body的读取方式问题,看是简 ...

  3. ASP.NET Core 文件上传

    前言 上篇博文介绍了怎么样在 asp.net core 使用 Redis 和 Protobuf 进行 Session缓存.本篇的是开发过程中使用的一个小功能,怎么做单文件和多文件上传. 如果你觉得对你 ...

  4. ASP.NET Core文件上传与下载(多种上传方式)

    前言 前段时间项目上线,实在太忙,最近终于开始可以研究研究ASP.NET Core了. 打算写个系列,但是还没想好目录,今天先来一篇,后面在整理吧. ASP.NET Core 2.0 发展到现在,已经 ...

  5. Asp.Net Core 文件上传处理

    本文主要介绍后台接收处理 1.在使用控制器接收 : [HttpPost] : public IActionResult UploadFiles(IList<IFormFile> files ...

  6. Asp.Net Core文件上传

    文件上传功能在实际开发中经常使用,在 .Net Core中,文件上传接收类型不再使用 HttpPostedFile 或 HttpFileCollection来接收,而是使用 IFormFile 或 I ...

  7. ASP.NET Core文件上传、下载与删除

    首先我们需要创建一个form表单如下: <form method="post" enctype="multipart/form-data" asp-con ...

  8. 解决ASP.NET Core Mvc文件上传限制问题

    一.简介 在ASP.NET Core MVC中,文件上传的最大上传文件默认为20MB,如果我们想上传一些比较大的文件,就不知道怎么去设置了,没有了Web.Config我们应该如何下手呢? 二.设置上传 ...

  9. .net core版 文件上传/ 支持批量上传,拖拽以及预览,bootstrap fileinput上传文件

    asp.net mvc请移步 mvc文件上传支持批量上传,拖拽以及预览,文件内容校验 本篇内容主要解决.net core中文件上传的问题  开发环境:ubuntu+vscode 1.导入所需要的包:n ...

随机推荐

  1. 【Idea】-NO.162.Idea.1 -【Idea Unable to import maven project: See logs for details】

    Style:Mac Series:Java Since:2018-09-10 End:2018-09-10 Total Hours:1 Degree Of Diffculty:5 Degree Of ...

  2. locust压测rpc协议

    这里主要是google的grpc接口进行压测的一个栗子. Locust是以HTTP为主要目标构建的. 但是,通过编写钩子触发器request_success和 request_failure事件的自定 ...

  3. 69.js--点击事件等比例弹出层div

    html:<!--弹出层导航栏--> <div class="public-nav-content"> <ul> <li><a ...

  4. 统计Oracle一个表空间中各个segment占用的空间大小

    SQL语句参考以下: select owner,segment_name,segment_type,tablespace_name,sum(bytes)/1024/1024/1024 from dba ...

  5. winsock I/O模型的分析

    几种winsock I/O模型的分析 套接字是通信的基础,是支持网络协议数据通信的基本接口.Winsocket 提供了一些有趣的I/O模型,有助于应用程序通过一种“异步”方式,一次对一个或者多个套接字 ...

  6. 自己绘制table,加分页

  7. Remastersys打包你自己的ubuntu成iso文件

    采用Remastersys3.0.4.ubuntu版本是ubuntu14.04 LTS amd64. (1)软件下载安装: 下载: 到http://www.easy-vdr.de/downloads/ ...

  8. redis 无序集合(set)函数

    sAdd 命令/方法/函数 Adds a value to the set value stored at key. If this value is already in the set, FALS ...

  9. 类与对象 && 继承

      以下是本人的对类与对象.继承的一些理解,如有错误之处万望谅解,如有朋友愿意指正,十分乐意,万分感谢!   类与对象     类与对象是学习编程的基础(大概吧),那么何为类?何为对象呢?   一.简 ...

  10. sql 与 mysql 链接数据库