Asp.Net Core文件上传IFormFile
2023-09-14 09:16:30 时间
文件上传功能在实际开发中经常使用,在 .Net Core中,文件上传接收类型不再使用 HttpPostedFile 或 HttpFileCollection来接收,而是使用 IFormFile 或 IFormFileCollection来接收。
下面看一个例子就明白怎么使用了,具体代码如下:
<div class="form-group">
<form enctype="multipart/form-data" asp-controller="home" asp-action="upload" method="post">
<input type="file" name="input" id="input" class="custom-file-input" />
<label class="custom-file-label" for="input"></label>
<input type="submit" value="提交" />
</form>
</div>
@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);
input.CopyTo(new FileStream(filePath, FileMode.Create));
return Ok();
}
}
}
多文件上传
<div class="form-group">
<form enctype="multipart/form-data" asp-controller="home" asp-action="upload" method="post">
<input type="file" name="input" id="input" class="custom-file-input" multiple />
<label class="custom-file-label" for="input"></label>
<input type="submit" value="提交" />
</form>
</div>
@section scripts{
<script>
$(document).ready(function () {
$(".custom-file-input").on("change", function () {
var fileLabel = $(this).next(".coustom-file-lable");
var files = $(this)[0].files;
if (files.length > 1) {
fileLabel.html("你已选择了" + files.length + "个文件");
} else {
fileLabel.html(files[0].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[0].FileName;
string filePath = Path.Combine(uploadsFolder,uniqueFileName);
input[0].CopyTo(new FileStream(filePath, FileMode.Create));
return Ok();
}
相关文章
- Linux.Net:开启新技术之旅(linux.net)
- .netLinux下部署ASP.NET环境指南(linux部署asp)
- 数据库使用.NET连接MySQL数据库(net连接mysql)
- Linux上安装.NET:提高开发效率,拓展技术栈(linux安装.net)
- Oracle NET突破技术界限,精彩开启新纪元(oracle net??)
- asp.net的web服务MSSQL检测ASP.NET的Web服务——利用它构建更棒的站点(mssql检测基于)
- NET与Oracle结合出现新的可能(.net oracle)
- 面向企业的Net框架开发与Oracle集成(net框架oracle)
- CMySQL与NET技术融会贯通(c mysql .net)
- NET环境下 MySQL数据库应用实践(.net配合 mysql)
- NET与MySQL实现无缝连接(.net mysql连接)
- NET 对MySQL 的支持有限(.net不支持mysql)
- asp.net(C#)动态添加非ASP的标准html控件(如添加Script标签)
- ASP.NET常用文件上传方法
- asp.net实现文件下载的代码
- 修改服务器配置让asp.net文件后缀名随心所欲
- 浅析ASP.NET安全性分析(加强asp.net1.1/2.0安全性)
- ASP.NET解决上传大文件问题的方法
- Asp.net在ashx文件中处理Session问题解决方法
- ASP.NET中Web.config文件的层次关系详细介绍
- asp.net上传execl文件后,在页面上加载显示(示例代码)
- asp.net文件上传示例分享
- ASP.net全局程序文件Global.asax用法分析
- Asp.net获取服务器指定文件夹目录文件并提供下载的方法
- Asp.net实现MVC处理文件的上传下载功能实例教程