.net core文件上传与下载

发布时间 2023-04-07 10:31:31作者: 沈先生爱猫咪

使用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>