分享web开发知识

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

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

Asp.net core 学习笔记 ( upload/download files 文件上传与下载 )

发布时间:2023-09-06 01:13责任编辑:胡小海关键词:upload文件上传

2017-09-25 

refer : https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads

https://www.codeproject.com/Articles/1203408/Upload-Download-Files-in-ASP-NET-Core

这里只说说小文件上传. 

先看看前端 js 代码 

<input id="inputFile" type="file" /><script> ???document.getElementById(‘inputFile‘).addEventListener(‘change‘, (e) => { ???????????????let files = e.target.files; ???????????????let formData = new FormData(); ???????formData.append("file", files[0]); ???????let http = new XMLHttpRequest(); ???????http.open(‘POST‘, ‘/upload-file‘, true); ???????http.send(formData); ????}, false); </script>

如果要上传多个文件就 append 多几个就是了 

c# 

public class UploadFileData{ ???public IFormFile file { get; set; }}[Area("Web")]public class UploadController : Controller{ ???public UploadController( ???????????IHostingEnvironment environment ???) ???{ ???????this.environment = environment; ???} ???private IHostingEnvironment environment { get; set; } ???????????[HttpPost("upload-file")] ???public async Task<IActionResult> uploadFile(UploadFileData data) ???{ ???????var allFiles = Request.Form.Files; // 多文件的话可以直接从 form 拿到完 ????????var root = environment.WebRootPath; ???????var extension = Path.GetExtension(data.file.FileName); ???????var guid = Guid.NewGuid().ToString(); ???????var fullPath = $@"{root}\images\{guid + extension}"; ???????using (FileStream stream = new FileStream(fullPath, FileMode.Create)) ???????{ ???????????await data.file.CopyToAsync(stream); ???????} ????????return Ok(); ???} ???[Route("upload")] ???public async Task<IActionResult> Index() ???{ ???????return View(); ???}}

上面的是最简单的版本,创建 file 然后把 upload file stream 写入 

还有一个常用的场景是, 上传图片要做 EXIF 处理. 

可以用 Magick.NET 目前支持 core, 不过呢, 好像只支持 window 场景. 如果你不是 windows 可能要在等等或则用其它插件.

using (var stream = data.file.OpenReadStream())using (MagickImage image = new MagickImage(stream)){ ???ExifProfile profile = image.GetExifProfile(); ???image.Settings.SetDefine(MagickFormat.Jpeg, "sampling-factor", "4:2:0"); ???image.Strip(); //这句会把图片的所有 EXIF 洗掉 ???image.Quality = 85; ???if (profile != null) ???{ ???????ExifValue orientation = profile.Values.SingleOrDefault(v => v.Tag == ExifTag.Orientation); ???????if (orientation != null) ???????{ ???????????int orientationInt = Convert.ToInt32(orientation.Value); ???????????if (orientationInt == 6) ???????????{ ???????????????image.Rotate(90); ???????????} ???????????else if (orientationInt == 8) ???????????{ ???????????????image.Rotate(-90); ???????????} ???????????else if (orientationInt == 8) ???????????{ ???????????????image.Rotate(180); ???????????} ???????} ???????image.Write(fullPath); ???} ???else ???{ ???????image.Write(fullPath); ???}}

很简单吧.

再来一个 zip 的 

using (var stream = data.file.OpenReadStream())using (var compressedFileStream = new FileStream($@"{root}\images\{guid}.zip", FileMode.Create))using (var zipArchive = new ZipArchive(compressedFileStream, ZipArchiveMode.Update, false)){ ???var zipEntry = zipArchive.CreateEntry(data.file.FileName); ???using (var zipEntryStream = zipEntry.Open()) ???{ ???????stream.CopyTo(zipEntryStream); ???}}

core 支持 ZipArchive 哦

下载也很容易 

public UploadController( ???????IHostingEnvironment environment, ???????IContentTypeProvider contentTypeProvider){ ???this.environment = environment; ???this.contentTypeProvider = contentTypeProvider;}private IHostingEnvironment environment { get; set; }private IContentTypeProvider contentTypeProvider { get; set; } ???????[HttpGet("download-file")]public FileResult downloadFile(string name, string display){ ???string contentType; ???contentTypeProvider.TryGetContentType(name, out contentType); ???HttpContext.Response.ContentType = contentType; ???string path = environment.WebRootPath + @"\images\" + name; // 注意哦, 不要像我这样直接使用客户端的值来拼接 path, 危险的 ????FileContentResult result = new FileContentResult(System.IO.File.ReadAllBytes(path), contentType) ???{ ???????FileDownloadName = display ???}; ???return result;}

html

<a href="/download-file?name=123.jpg&display=aaa.jpg" download ?>download</a>

Asp.net core 学习笔记 ( upload/download files 文件上传与下载 )

原文地址:http://www.cnblogs.com/keatkeat/p/7594496.html

知识推荐

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