.net core文件上传与下载
使用Asp.Net Core 进行文件的上传与下载
控制器代码如下
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace UploadFileTest.Controllers
{
public class UploadFileController : Controller
{
private readonly IWebHostEnvironment hostEnvironment;
public UploadFileController(IWebHostEnvironment hostEnvironment)
{
this.hostEnvironment = hostEnvironment;
}
//返回页面
public IActionResult Index()
{
return View();
}
///单文件上传
[HttpPost]
public IActionResult SingleFile(IFormFile file)
{
string filePath = Path.Combine(hostEnvironment.ContentRootPath, "Upload", file.FileName);
using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
file.CopyTo(fs);
}
return Ok("上传成功");
}
//多文件上传
[HttpPost]
public IActionResult MoreFile(IFormFile[] files)
{
foreach (IFormFile file in files)
{
string filePath = Path.Combine(hostEnvironment.ContentRootPath, "Upload", file.FileName);
using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
file.CopyTo(fs);
}
}
return Ok("上传成功");
}
//结合表单上传
[HttpPost]
public IActionResult FormWithFile(Student student)
{
string filePath = Path.Combine(hostEnvironment.ContentRootPath, "Upload", student.Image.FileName);
//做添加操作....
using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
student.Image.CopyTo(fs);
}
return Ok(student.Name + " " + student.Gender); ;
}
//文件下载
public IActionResult GetFile()
{
string fileName = "OIP-C.jfif";
string file = Path.Combine(hostEnvironment.ContentRootPath, "Upload", fileName);
FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
return File(fs, "application/octet-stream", fileName);
}
}
public class Student
{
public string Name { get; set; }
public string Gender { get; set; }
public IFormFile Image { get; set; }
}
}
视图如下
@model UploadFileTest.Controllers.Student
<div>
<h2>单文件上传</h2>
<form action="/UploadFile/SingleFile" method="post" enctype="multipart/form-data">
@Html.Label("file", "文件上传")
@Html.TextBox("file", "", htmlAttributes: new { type = "file", name = "file" })
<input type="submit" value="提交" />
</form>
</div>
<div>
<h2>多文件上传</h2>
<form action="/UploadFile/MoreFile" method="post" enctype="multipart/form-data">
@Html.Label("files", "文件上传")
@Html.TextBox("files", "", htmlAttributes: new { type = "file", multiple = "multiple" })
<input type="submit" value="提交" />
</form>
</div>
<div>
<h2>结合表单文件上传</h2>
<form action="/UploadFile/FormWithFile" method="post" enctype="multipart/form-data">
<div>
@Html.LabelFor(x => x.Image)
@Html.TextBoxFor(x => x.Image, htmlAttributes: new { type = "file" })
</div>
<div>
@Html.LabelFor(x => x.Name)
@Html.TextBoxFor(x => x.Name)
</div>
<div>
<label>
男
@Html.RadioButton("Gender", "男")
</label>
<label>
女
@Html.RadioButton("Gender", "女")
</label>
</div>
<input type="submit" value="提交" />
</form>
</div>
<div>
<h2>文件下载</h2>
@Html.ActionLink("下载文件","GetFile")
</div>
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析