首页 > Web开发 > 详细

Web项目访问在C盘的图片,不在项目路径下的文件

时间:2020-04-18 17:32:23      阅读:144      评论:0      收藏:0      [点我收藏+]
使用aspx方式
 
1.前台显示
<img src="/UeImg.aspx?path=C:/YxFile/ueditor/upload/image/20200211/6371705083711732189358173.jpg" title="1.jpg" _src="/UeImg.aspx?path=C:/YxFile/ueditor/upload/image/20200211/6371705083711732189358173.jpg" alt="1.jpg">

 

2.后台代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UeImg.aspx.cs" Inherits="VanChuam.Application.Web.Areas.LH_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 VanChuam.Application.Web.Areas.LH_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
    }
}

 

Web项目访问在C盘的图片,不在项目路径下的文件

原文:https://www.cnblogs.com/masonblog/p/12726697.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!