Web项目访问在C盘的图片(不在当前项目路径下的图片)

使用ASPX页面处理

前台显示

<img src="/UeImg.aspx?path=C:/YxFile/ueditor/upload/image/20200211/6371705083711732189358173.jpg" title="1.jpg" alt="1.jpg">

后台代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UeImg.aspx.cs" Inherits="MyTest.Application.Web.Areas.My_YingXiao.Views.YxNews.UeImg" %>

aspx.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyTest.Application.Web.Areas.My_YingXiao.Views.YxNews
{
    public partial class UeImg : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var filePath = GetParam("path");//文件的完整路径
            if (!File.Exists(filePath))
            {
                return;
            }
            var bmp = new Bitmap(filePath);
            //清除该页输出缓存,设置该页无缓存 
            Context.Response.Buffer = true;
            Context.Response.ExpiresAbsolute = DateTime.Now.AddMilliseconds(0);
            Context.Response.Expires = 0;
            Context.Response.CacheControl = "no-cache";
            Context.Response.AppendHeader("Pragma", "No-Cache");
            //将验证码图片写入内存流,并将其以 "image/Png" 格式输出 
            var ms = new MemoryStream();
            try
            {
                bmp.Save(ms, ImageFormat.Jpeg);
                Context.Response.ClearContent();
                Context.Response.ContentType = "image/jpeg";
                Context.Response.BinaryWrite(ms.ToArray());
            }
            finally
            {
                //显式释放资源 
                bmp.Dispose();
            }
        }

        #region 获取REQUEST的参数值
        /// <summary>
        /// 获取REQUEST的参数值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        protected string GetParam(string key)
        {
            if (Context.Request[key] == null)
                return string.Empty;
            return Context.Request[key].ToString(CultureInfo.InvariantCulture).Trim() + "" == ""
                       ? string.Empty
                       : Context.Server.HtmlEncode(Context.Request[key].Trim());
        }
        #endregion
    }
}
原文地址:https://www.cnblogs.com/masonblog/p/12726697.html