文件上传功能在实际开发中经常使用,在 .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. Planet.Earth.II.EP05.2016

  2. 2G以上的大文件如何上传

    无法上传大文件是因为php.ini配置有限制了,这样限制了用户默认最大为2MB了,超过了就不能上传了,如果你确实要上传我们可以按下面方法来处理一下. 打开php.ini, 参数  设置  说明 fil ...

  3. luogu 1144

    最短路计数 #include <bits/stdc++.h> using namespace std; , M = 2e6 + ; << ); #define gc getch ...

  4. 块状链表 bzoj 3343教主的魔法

    //块状链表//分块排序,然后每次查找时在暴力查找头和尾两个块.//中间那些块,因为有序所以只需2分查找即可.我用的是lower_pound();//插入是,也是头和尾暴力插入,中间那些加到一个累计里 ...

  5. 解决一些python的问题记录

    1.python3中出现ModuleNotFoundError: No module named 'pkg_resources' wget https://bootstrap.pypa.io/ez_s ...

  6. window.postMessage 跨窗口,跨iframe javascript 通信

    同源通信 执行它们的页面位于具有相同的协议(http/https),端口(80/443),主机(通常为域名) 时,这两个脚本才能相互通信 大多数情况下,网站就是内部的域名,所以是同源通信,可以相互访问 ...

  7. Linux中 mv(文件移动)

    mv命令的功能有以下两种: source target mv 参数1 参数2 1.对文件或目录重新命名 如果源文件和目标文件在同一个目录下,mv的作用就是改文件名. 2.将文件从一个目录移到另一个目录 ...

  8. Redis 命令使用

    Redis 中所有 key-value 都储存在 Redis-Object 中,Redis-Object 主要信息有: 数据类型(type) string (字符串) hash (Hash表) lis ...

  9. windows系统下Jenkins 持续集成安装使用

    先要下载安装Tomcat,基于Java的web项目可以通过Tomcat运行.下载Jenkins,注意要安装在Tomcat的webapps目录下.安装完Jenkins会自动通过浏览器打开http://l ...

  10. Maven中的SnapShot版本和Release版本

    # Maven中的SnapShot版本和Release版本 ## 区别 - SnapShot 快照版本- Release 发布版本 ## 重要区别 - 本地获取这些依赖的机制不同,如果是快照版本,Ma ...