当前位置: 首页 > news >正文

C# MVC controller 上传附件及下载附件(笔记)

描述:Microsoft.AspNetCore.Http.IFormFileCollection 实现附件快速上传功能代码。

上传附件代码

        [Route("myUploadFile")][HttpPost]public ActionResult MyUploadFile([FromForm] upLoadFile rfile){string newFileName = Guid.NewGuid().ToString("N") + rfile.mingcheng;string dirPath = Path.Combine(new string[] { Environment.CurrentDirectory, "wwwroot", "File", "FileUpload", newFileName });if (System.IO.Directory.Exists(dirPath)){System.IO.Directory.CreateDirectory(dirPath);System.Threading.Thread.Sleep(0);}string filePath = Path.Combine(new string[] { dirPath, newFileName });var t = Request.Form.Files[0];using (FileStream fs = System.IO.File.Open(filePath, FileMode.OpenOrCreate)){string fileName = rfile.files[0].FileName;byte[] bs = new byte[rfile.files[0].Length];GetPicThumbnail(rfile.files[0].OpenReadStream()).Read(bs, 0, bs.Length);fs.Write(bs, 0, bs.Length);fs.Close();}return new ObjectResult(new { code = "1", msg = "文件上传成功" });}

upLoadFile Model类定义

    public class upLoadFile{public string mingcheng { get; set; }public Microsoft.AspNetCore.Http.IFormFileCollection files { get; set; }public string miaoshu { get; set; }}

附件下载代码:

      [Route("myGetFile")][HttpGet]public ActionResult MyGetFile(string id){PhysicalFileResult f = new PhysicalFileResult(@"E:\work\codes\技术积累\临时\weixinshare\weixinshare\weixinshare\wwwroot\File\weixinimg\2edf0b9324cb44edaa7d73ea46ab0b15.jpg", "application/file");f.FileDownloadName = "ddd.jpg";f.EnableRangeProcessing = true;f.LastModified = DateTimeOffset.Now;return f;}
http://www.lryc.cn/news/113741.html

相关文章:

  • 安装element-plus报错:Conflicting peer dependency: eslint-plugin-vue@7.20.0
  • 【操作系统】进程和线程对照解释
  • 4用opencv玩转图像2
  • Swagger的使用
  • python高阶技巧
  • Linux和Windows安装MySQL服务
  • Vue3 第四节 自定义hook函数以及组合式API
  • 门面模式(C++)
  • ASP.NET Core SignalR
  • auto-changelog的简单使用
  • map 比较(两个map的key,value 是否一样)
  • LayUI之入门
  • 【Linux】Linux下git的使用
  • QT读写配置文件
  • 【计算机网络】12、frp 内网穿透
  • Pytest 重复执行用例插件----pytest-repeat
  • 【软件工程】5 ATM系统测试
  • opencv读取MP4文件和摄像头数据
  • Qt实现自定义QDoubleSpinBox软键盘
  • 小研究 - 微服务系统服务依赖发现技术综述(一)
  • 2023-08-07力扣今日八题
  • Segment Anything【论文翻译】
  • 银河麒麟QT连接DM8数据库
  • 并发编程1:线程安全性概述
  • (论文复现)DeepAnt模型复现及应用
  • 【机器学习】在 MLOps构建项目 ( MLOps2)
  • 【MySQL】聚合函数与分组查询
  • conda 环境 numpy 安装报错需要 Microsoft Visual C++ 14.0
  • 算法工程师-机器学习面试题总结(5)
  • 论文阅读 RRNet: A Hybrid Detector for Object Detection in Drone-captured Images