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
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM