• asp.net core mvc 文件上传,下载,预览


    //文件上传用到了IformFile接口
    1.1文件上传视图

    <form action="/stu/upload" method="post" enctype="multipart/form-data">
        <input type="file" name="img" />
        <input type="submit" value="上传" />
    </form>
    
    • 1
    • 2
    • 3
    • 4

    1.2文件上传后端代码

      private readonly IWebHostEnvironment _webHostEnvironment;
    
            public StuController(IWebHostEnvironment webHostEnvironment)
            {
                //使用内置的服务读取项目路径
                _webHostEnvironment = webHostEnvironment;
            }
            public IActionResult upload()
            {
                return View();
            }
    
            [HttpPost]
            public IActionResult upload(IFormFile img)
            {
                //返回根目录
                var contentpath = _webHostEnvironment.ContentRootPath;
                //项目跟目录下创建一个upload文件夹,存放上传的图片
                var filepath = Path.Combine(contentpath, "upload", img.FileName);
                //创建一个文件流
                using (FileStream fs = new FileStream(filepath, FileMode.Create, FileAccess.Write))
                    //把上传的文件写入文件中
                    img.CopyTo(fs);
                return Content("上传成功");
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    1.3 运行效果
    在这里插入图片描述
    2.1下载文件视图

    @Html.ActionLink("下载","downloadImg")
    
    • 1

    2.2后端代码(我这写死了,实际项目是前端传参下载图片的参数到后端)

     public byte[] GetImageBytes()
            {
                var rootpath = _webHostEnvironment.ContentRootPath;
                var filepath = Path.Combine(rootpath, "upload", "冒泡排序.png");
                string imagePath = filepath;
                byte[] imageBytes;
    
                try
                {
                    using (FileStream fs = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
                    {
                        using (BinaryReader br = new BinaryReader(fs))
                        {
                            imageBytes = br.ReadBytes((int)fs.Length);
                        }
                    }
                }
                catch (Exception ex)
                {
                    // 处理异常
                    Console.WriteLine("读取图片文件时出错:" + ex.Message);
                    imageBytes = null;
                }
    
                return imageBytes;
            }
            public IActionResult downloadImg()
            {
                byte[] imageBytes = GetImageBytes(); // 从某处获取图片的字节数组
                string fileName = "image.jpg";
                return File(imageBytes, "image/png", fileName);
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    3.1预览图片视图

    <img src="@Url.Action("GetImage", "stu", new { imageName = "4.jpg" })" alt="Image Preview">
    <style>
        img{
            width:300px;
            height:400px;
        }
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    3.2后端代码

     public IActionResult GetImage(string imageName)
            {
                var rootpath = _webHostEnvironment.ContentRootPath;
                var imagePath = Path.Combine(rootpath, "upload", imageName);
                var imageBytes = System.IO.File.ReadAllBytes(imagePath);
                return File(imageBytes, "image/jpeg");
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    运行效果
    在这里插入图片描述

  • 相关阅读:
    [天翼杯 2021]esay_eval - RCE(disabled_function绕过||AS_Redis绕过)+反序列化(大小写&wakeup绕过)
    请求和响应
    设计模式-行为型模式-责任链模式
    API安全防御建设中的难点坑点汇总
    获取文件信息: 大小, 文件类型等.
    完犊子!原单位的离职证明丢了,下周要入职了,用AI做一个行不行?
    GPT4RoI: Instruction Tuning Large Language Model on Region-of-Interest
    Tomcat部署及优化
    react组件多次渲染问题
    Tomcat部署及优化
  • 原文地址:https://blog.csdn.net/qq_41942413/article/details/133421155