.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 ...
随机推荐
- stm32定时器输出移相PWM(非主从模式)
背景:由于项目需要,需要stm32输出任意相角度的PWM 前提知识: 1.stm32定时器的Tim,一般有多个OC.具体差别根据型号来定. 2.定时器的使能,理论上是多个通道同时使能 3.TIM_OC ...
- c# 有序链表合并 链表反转
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Spring Security 内置过滤器表
别名 Filter 类 CHANNEL_FILTER ChannelProcessingFilter SECURITY_CONTEXT_FILTER SecurityContextPersistenc ...
- FB面经Prepare: Email User
有一些账号,账号里面有一个或多个email, 如果两个账号有共同的email,则认为这两个账号是同一个人,找出哪些账号是同一个人 输入是这样的:数字是用户,字母是邮箱,有很多人有多个邮箱,找出相同的用 ...
- 007-li标签CSS水平居中垂直居中
水平居中是text-align:center垂直居中 一般是用 line-height比如你li的高度是80px 那你设置 line-height:80px 文字就垂直居中
- sql 查询所有表以及表结构
查询数据库中所有的表: as statu from [sysobjects] where [type] = 'u' order by [name] 查询表结构: --查询表结构 ALTER PROCE ...
- spring boot 如何添加拦截
添加拦截其它挺简单的,直接上代码吧,我以简单的登陆验证拦截为例 1,先实现一个拦截器 package com.xinyue.interview.gm.filter; import javax.serv ...
- Original Autel MaxiSys Pro MS908P support 2 Year Free Update Online
You can get 2 Year Free Update Online once you place an order on Autel MS908P. Software Version: Eve ...
- pyCharm最新2018激活码
本教程对jetbrains全系列可用例:IDEA.WebStorm.phpstorm.clion等 因公司的需求,需要做一个爬取最近上映的电影.列车号.航班号.机场.车站等信息,所以需要我做一个爬虫项 ...
- [dart学习]第二篇:dart变量介绍 (一)
前言 本文的所有内容均是官方文档的简单翻译和理解,需要查看原文,请登录 https://www.dartlang.org/guides/language/language-tour 阅读, 让我们 ...