文件上传功能在实际开发中经常使用,在 .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文件上传的更多相关文章

  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文件上传、下载与删除

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

  7. asp.net core分块上传文件

    写完asp.net多文件上传(http://www.cnblogs.com/bestckk/p/5987383.html)后,感觉这种上传还是有很多缺陷,于是...(省略一万字,不废话).这里我没用传 ...

  8. ASP.NET多文件上传实例

    在Web应用程序开发中,避免不了要用到上传文件这个功能,但以前上传文件是个很麻烦的事,现在有了.NET,文件上传变得轻而易举.下面的这个例子实现了多文件上传功能.可以动态添加输入表单,上传的文件数量没 ...

  9. ASP.NET - 多文件上传,纯代码,不使用插件

    解决方案: 前段代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Mu ...

随机推荐

  1. Codeforces Global Round 4

    目录 Contest Info Solutions A. Prime Minister B. WOW Factor C. Tiles D. Prime Graph E. Archaeology F1. ...

  2. Tkinter 之ScrollBar滚动条标签

    一.参数说明 参数 作用 background (bg) 设置背景颜色 borderwidth (bd) 指定边框宽度,通常是 2 像素 cursor  指定当鼠标在上方飘过的时候的鼠标样式 orie ...

  3. ICEMCFD中,face裂缝修复的小窍门【转载】

    转载自:http://blog.sina.com.cn/s/blog_4a21884b010005ng.html 采用ICEMCFD画网格的初学者,都对由cad(proe/ug/solidworks) ...

  4. HttpClient学习(四)—— 关于Http

    一.Http状态码 状态码分类 100 ~ 199 信息提示 200 ~ 299 成功 300 ~ 399 重定向 400 ~ 499 客户端错误 500 ~ 599 服务端错误 常见状态码 200 ...

  5. 备份的数据库文件(500M左右)无法导入的解决方法

    解决方法: 修改配置文件/usr/local/mysql/my.cnf 在my.cnf文件下添加一句:max_allowed_packet=900M 注:此处大小不能设置过大,过大可能会导致还原过程中 ...

  6. golang 不足

    滴滴出行技术总监:关于技术选型的那些事儿 原创: 杜欢 InfoQ 2017-02-26   https://mp.weixin.qq.com/s/6EtLzMhdtQijRA7Xrn_pTg     ...

  7. element ui组件的开始时间-结束时间验证

    <el-date-picker v-model="seach.before" type="date" placeholder="开始时间&quo ...

  8. CV2——学习笔记-图像分类

    1.图像分类 • 2.神经网络原理 • 1.卷积神经网络介绍 • 2.利用caffe搭建深度网络做图像分类 挑战:光照变化+形变.类内变化. 标签.预测函数,泛化能力. 如何提高泛化能力?需要用图像特 ...

  9. Android:Mstar平台 HDMI OUT 静音流程

    一.framework层 1. APP调用 AudioManager 的 adjustStreamVolume() 接口实现在: frameworks\base\services\core\java\ ...

  10. Python Re 模块超全解读

    re模块下的函数 compile(pattern):创建模式对象 import re pat=re.compile('A') m=pat.search('CBA') #等价于 re.search('A ...