.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多进程web爬虫-提升性能利器
背景介绍: 小爬我最近给部门开发了一系列OA的爬虫工具,从selenium前端模拟进化到纯requests后台post请求爬取,效率逐步提升.刚开始能维持在0.5秒/笔.可惜当数据超过2000笔后,爬 ...
- 谷歌将一些弱小的库从安卓代码移除Google Removes Vulnerable Library from Android
Google this week released the November 2018 set of security patches for its Android platform, which ...
- Python练习--普通函数与递归函数求阶乘
'''Created on 2018年10月28日递归函数示例:阶乘'''def my_fun_example1(n): ''' 非递归函数求阶乘示例 ''' result = ...
- 运行main方法找不到类
http://blog.csdn.net/chenleixing/article/details/44816629
- vue中样式的典型操作(:class,:style)
<template> <div class="home-wrapper"> <div class="home-top">th ...
- Kindle:自动追更之发送邮件
@echo off setlocal enabledelayedexpansion set from=Kindlekindle设置好信任的邮箱set pw=密码 set to=Kindle邮箱 cd ...
- OO第二单元优化博客
OO第二单元优化博客 第五次作业没有性能分,但是,我在这一单元的宗旨就是写一个日常生活中 最常见的那种电梯,所以第五次我没有写傻瓜电梯,而是直接写了个\(look\),和第六次基本相同. 总计一下lo ...
- 对象序列化Serializable
一.Java对象的存储 首先我们先来理解一下Java对象在内存中的存储! JVM的内存分为三个部分:栈(stack).堆栈(heap).方法区(method area): 栈:主要存储基本数据类型变量 ...
- new Date().getTime()和System.currentTimeMillis()的区别
在Java中,new Date().getTime()和System.currentTimeMillis()都是用来获取当前时间的,并可以用DateFormat转成对应的时间格式,代码如下. impo ...
- MTK-TP(触屏)解读一
MTK中的TP代码结构并不复杂,相比于其他的系统更为的简单些.它使用的是input子系统,通过该系统来上报触摸按键. 首先我们来看看TP的文件夹下的各代码文件的功能. 文件名 具体功能 关系文件 tp ...