General.Web/Controllers/Web/ImagesController.cs

(开头部分) 5KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;

namespace General.Mvc.Controllers.Web
{
    public class ImagesController : Controller
    {
        private readonly IWebHostEnvironment webHostEnvironment;

        public ImagesController(IWebHostEnvironment webHostEnvironment)
        {
            this.webHostEnvironment = webHostEnvironment;
        }


        /// <summary>
        /// 缩略图获取接口
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        //[Route("/images/{name}")]
        public async Task<IActionResult> GetPath(string name="", int width = 100)
        {
            if (name.StartsWith("/upload"))
            {
                name = name.Replace("/upload","");
            }
            var appPath = Path.Combine(webHostEnvironment.WebRootPath + "/upload");
            var errorImage = Path.Combine(appPath, "404.png");//没有找到图片
            var imgPath = string.IsNullOrEmpty(name) ? errorImage : Path.Combine(appPath + name);
            //获取图片的返回类型
            var contentTypDict = new Dictionary<string, string> {
                {"jpg","image/jpeg"},
                {"jpeg","image/jpeg"},
                {"jpe","image/jpeg"},
                {"png","image/png"},
                {"gif","image/gif"},
                {"ico","image/x-ico"},
                {"tif","image/tiff"},
                {"tiff","image/tiff"},
                {"fax","image/fax"},
                {"wbmp","image//vnd.wap.wbmp"},
                {"rp","image/vnd.rn-realpix"}
            };
            var contentTypeStr = "image/jpeg";
            var imgTypeSplit = name.Split('.');
            var imgType = imgTypeSplit[imgTypeSplit.Length - 1].ToLower();
            //未知的图片类型
            if (!contentTypDict.ContainsKey(imgType))
            {
                imgPath = errorImage;
            }
            else
后面还有 90 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。