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 ...
随机推荐
- xml介绍+xml创建+xml读取
1.xml介绍:(URL:https://blog.csdn.net/weixin_37861326/article/details/81082144) xml是用来传输内容的,是w3c推荐的 2.使 ...
- 容器网络启用RDMA高速通讯-Freeflow
容器网络启用RDMA高速通讯-Freeflow 容器网络启用RDMA高速通讯-Freeflow 本文编译自: Freeflow,https://github.com/openthings/Freefl ...
- 【原创】go语言学习(十)Map类型
目录 声明和初始化 基本操作 map排序 map类型的切⽚片 课后作业 声明和初始化 1.map类型是⼀一个key-value的数据结构. //var a map[key的类型]value类型 var ...
- 从输入URL到浏览页面的过程
之前我们已经讨论过浏览器的渲染原理,今天我们来讨论下更广泛的从输入URL到渲染出页面的过程. 1. 查询该URL是否有缓存 如果有,则直接返回,没有的话,下一步 2. 查询URL对应的IP 首先,到 ...
- codeforces514E
Darth Vader and Tree CodeForces - 514E When Darth Vader gets bored, he sits down on the sofa, closes ...
- python快捷键的使用【摘抄】
接触python有些快捷键还不熟悉,搜索到下面这个文章很好的转发和摘抄了,感谢作者的用心分析 摘抄来源:https://www.cnblogs.com/haiyan123/p/7170593.html ...
- vue-cli3.0的记录
页面打包的话,需要在根目录创建一个js文件 vue.confing.js 打包app的话,在dist里面只拿自己需要的静态文件
- sqlserver 两 表 数据 复制 (附加 跨服务器 查询的方法)
一 : 这个sql 语句 可以快速的 将 一 个旧表 中的指定字段的数据 复制到 另一个新表的指定字段中 insert into dbo.Customer ( CustomerId , Custome ...
- ICEMCFD中,face裂缝修复的小窍门【转载】
转载自:http://blog.sina.com.cn/s/blog_4a21884b010005ng.html 采用ICEMCFD画网格的初学者,都对由cad(proe/ug/solidworks) ...
- 第四章 基本TCP套接字编程 第五章 TCP客户/服务器程序实例
TCP客户与服务器进程之间发生的重大事件时间表 TCP服务器 socket() --- bind() --- listen() --- accept() --- read() --- write -- ...