Asp.Net Core文件上传
文件上传功能在实际开发中经常使用,在 .Net Core中,文件上传接收类型不再使用 HttpPostedFile 或 HttpFileCollection来接收,而是使用 IFormFile 或 IFormFileCollection来接收。
下面看一个例子就明白怎么使用了,具体代码如下:
<form enctype="multipart/form-data" asp-controller="home" asp-action="upload" method="post" class="form-horizontal">
<div class="form-group">
<label for="input" class="col-sm-2 control-label">头像</label>
<div class="col-sm-5">
<input type="file" name="input" id="input" class="custom-file-input"/>
<label class="custom-file-label"></label>
</div>
<input type="submit" value="提交" />
</div>
</form>
@section scripts{
<script>
$(document).ready(function () {
$(".custom-file-input").on("change", function () {
var fileName = $(this).val().split("\\").pop();
$(this).next(".custom-file-label").html(fileName);
})
});
</script>
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using FileUpload.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Hosting;
using System.IO; namespace FileUpload.Controllers
{
public class HomeController : Controller
{
private readonly IHostingEnvironment _hostingEnvironment; public HomeController(IHostingEnvironment hostingEnvironment)
{
_hostingEnvironment = hostingEnvironment;
} public IActionResult Index()
{
return View();
} [HttpPost]
public IActionResult Upload(IFormFile input)
{
if (input == null) return BadRequest(); string uploadsFolder = Path.Combine(_hostingEnvironment.WebRootPath, "images");
string uniqueFileName = Guid.NewGuid().ToString() + "_" + input.FileName; string filePath = Path.Combine(uploadsFolder,uniqueFileName);
using(FileStream fileStream = new FileStream(filePath,FileMode.Create)
{
input.CopyTo(fileStream);
} return Ok();
}
}
}
多文件上传
<form enctype="multipart/form-data" asp-controller="home" asp-action="upload" method="post" class="form-horizontal">
<div class="form-group">
<label for="input" class="col-sm-2 control-label">头像</label>
<div class="col-sm-5">
<input type="file" name="input" id="input" class="custom-file-input" multiple />
<label class="custom-file-label"></label>
</div>
<input type="submit" value="提交" />
</div>
</form>
@section scripts{
<script>
$(document).ready(function () {
$(".custom-file-input").on("change", function () {
var fileLabel = $(this).next(".coustom-file-lable");
var files = $(this)[].files;
if (files.length > ) {
fileLabel.html("你已选择了" + files.length + "个文件");
} else {
fileLabel.html(files[].name);
}
})
});
</script>
}
[HttpPost]
public IActionResult Upload(IFormFileCollection input)
{
if (input == null) return BadRequest(); string uploadsFolder = Path.Combine(_hostingEnvironment.WebRootPath, "images");
string uniqueFileName = Guid.NewGuid().ToString() + "_" + input[].FileName; string filePath = Path.Combine(uploadsFolder,uniqueFileName);
using(FileStream fileStream = new FileStream(filePath,FileMode.Create)
{
input[0].CopyTo(fileStream);
}
return Ok();
}
Asp.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文件上传、下载与删除
首先我们需要创建一个form表单如下: <form method="post" enctype="multipart/form-data" asp-con ...
- asp.net core分块上传文件
写完asp.net多文件上传(http://www.cnblogs.com/bestckk/p/5987383.html)后,感觉这种上传还是有很多缺陷,于是...(省略一万字,不废话).这里我没用传 ...
- ASP.NET多文件上传实例
在Web应用程序开发中,避免不了要用到上传文件这个功能,但以前上传文件是个很麻烦的事,现在有了.NET,文件上传变得轻而易举.下面的这个例子实现了多文件上传功能.可以动态添加输入表单,上传的文件数量没 ...
- ASP.NET - 多文件上传,纯代码,不使用插件
解决方案: 前段代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Mu ...
随机推荐
- Redis学习之一--基础知识
一.定义 REmote DIctionary Server(Redis) 是一个以字典结构存储数据的key-value存储系统:使用ANSI C语言编写.遵守BSD协议.支持网络.可基于内存亦可持久化 ...
- Vuex框架原理与源码分析
Vuex是一个专为Vue服务,用于管理页面数据状态.提供统一数据操作的生态系统.它集中于MVC模式中的Model层,规定所有的数据操作必须通过 action - mutation - state ch ...
- Shadows 使用说明
1:下载最新版 Windows地址:点击下载 Mac地址:点击下载 2:Windows安装插件(点击下方插件名即可下载) .NET Framework 4.7.2和 Microsoft Visual ...
- [报错解决] "MySQL server has gone away" 解决思路
大概的4个思路 1.超时,超时的阀值有wait_timeout这个参数控制 2.连接被人为的kill 3.发送的SQL语句过大超过max_allowed_packet的大小. (操作的sql语句太长了 ...
- 有没有一个工具可以帮助查找python的bug和进行静态的代码分析?
答:PyChecker是一个python代码的静态分析工具,它可以帮助查找python代码的bug, 会对代码的复杂度和格式提出警告 Pylint是另外一个工具可以进行codingstandard检查
- Ubuntu系统下安装完成tomcat进入管理页面
首先先启动tomcat cd /usr/local/tomcat8. ./bin/startup.sh 然后再打开浏览器 在地址栏中输入 http:/localhost:
- tomcat设置gzip
使用tomcat发布3dtiles或terrain数据遇到的gzip问题 问题一 对大于1KB的json请求进行gzip压缩,json为原文件 1.创建原始文件 2.设置 在apache-tomcat ...
- js小脚本解析后台数据
java代码 List<CodeTableBean> clfsList = StandardCodeTable.getCodeTable("clfs", "& ...
- Dockerfile HEALTHCHECK详解
Dockerfile中使用HEALTHCHECK的形式有两种: 1.HEALTHCHECK [options] CMD command(本次详细解释) 2.HEALTHCHECK NODE 意思是禁止 ...
- Jenkins 更新 jenkins.war的方法
Jenkins 有时候更新,直接是主页提示下载 jenkins.war只需要把下载的jenkins.war 替换原来的jenkins.war 就可以了那么问题来了? 原来的 jenkins.war 到 ...