分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

ASP.NET Core文件上传、下载与删除

发布时间:2023-09-06 02:05责任编辑:蔡小小关键词:.NET文件上传

首先我们需要创建一个form表单如下:

<form method="post" enctype="multipart/form-data" asp-controller="UpLoadFile" asp-action="FileSave">

        <div>

            <div>

                <p>Form表单多个上传文件:</p>

                <input type="file" name="files" multiple />

                <input type="submit" value="上传" />

            </div>

        </div>

    </form>

private IHostingEnvironment _hostingEnvironment;
???????public HomeController(IHostingEnvironment hostingEnvironment)
???????{

???????????_hostingEnvironment = hostingEnvironment;
???????}

//[RequestSizeLimit(100_000_000)] //最大100m左右
???????//[DisableRequestSizeLimit] ?//或者取消大小的限制
???????public IActionResult Upload()
???????{
???????????var files = Request.Form.Files;

???????????long size = files.Sum(f => f.Length);

???????????string webRootPath = _hostingEnvironment.WebRootPath;

???????????string contentRootPath = _hostingEnvironment.ContentRootPath;
???????????List<string> filenames=new List<string>();
???????????foreach (var formFile in files)

???????????{

???????????????if (formFile.Length > 0)

???????????????{
???????????????????string fileExt = Path.GetExtension(formFile.FileName); //文件扩展名,不含“.”

???????????????????long fileSize = formFile.Length; //获得文件大小,以字节为单位

???????????????????string newFileName = System.Guid.NewGuid().ToString()+ fileExt; //随机生成新的文件名

???????????????????var filePath = webRootPath + "/upload/";
???????????????????if (!Directory.Exists(filePath))
???????????????????{
???????????????????????Directory.CreateDirectory(filePath);
???????????????????}
???????????????????using (var stream = new FileStream(filePath+ newFileName, FileMode.Create))

???????????????????{
???????????????????????formFile.CopyTo(stream);
???????????????????}
???????????????????filenames.Add(newFileName);
???????????????}

???????????}

???????????return Ok(new { filenames, count = files.Count,size });
???????}
???????public IActionResult DownLoad(string file)

???????{
???????????string webRootPath = _hostingEnvironment.WebRootPath;
???????????var addrUrl = webRootPath+"/upload/"+ file;

???????????var stream = System.IO.File.OpenRead(addrUrl);

???????????string fileExt = Path.GetExtension(file);

???????????//获取文件的ContentType

???????????var provider = new FileExtensionContentTypeProvider();

???????????var memi = provider.Mappings[fileExt];

???????????return File(stream, memi, Path.GetFileName(addrUrl));

???????}

???????public IActionResult DeleteFile(string file)
???????{
???????????string webRootPath = _hostingEnvironment.WebRootPath;
???????????var addrUrl = webRootPath + "/upload/" + file;
???????????if (System.IO.File.Exists(addrUrl))
???????????{
???????????????//删除文件
???????????????System.IO.File.Delete(addrUrl); ??????????????
???????????}
???????????return Ok(new { file });
???????}

ASP.NET Core文件上传、下载与删除

原文地址:https://www.cnblogs.com/cb521413/p/9366156.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved