Files
g.hnyhua.cn/Mtxfw.shop/GetFiles.ashx.cs

168 lines
6.2 KiB
C#
Raw Permalink Normal View History

using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
namespace Mtxfw.shop
{
public class GetFiles : IHttpHandler
{
//参数列表附件ID缩略图大小缩略图质量
// aid itsize itql
//sample GetFiles.aspx?image=2009\8\29\090829235E70C09C1A31427DBF837E4C01847FA8.jpg&itsize=100x100&itql=80
Mtxfw.Utility.Config config = new Mtxfw.Utility.Config();
Mtxfw.Utility.QueryString Query = new Mtxfw.Utility.QueryString();
public void ProcessRequest(HttpContext context)
{
string filepath = "";
string f = Query.Get("f", "");
if (f != "")
{
filepath = Path.Combine(config.webUpPath, f);
}
else
{
filepath = GetFullFilePathByPath();
}
HttpContext.Current.Response.Write(filepath);
SendFile(HttpContext.Current.Server.MapPath(filepath));
}
protected void SendFile(string AFileName)
{
if (AFileName == "")
{
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.Write("&nbsp;&nbsp;&nbsp;ERROR<hr />文件不存在或已被删除! ");
System.Web.HttpContext.Current.Response.End();
return;
}
System.Web.HttpContext.Current.Response.Clear();
SetContentType(AFileName);
try
{
System.Web.HttpContext.Current.Response.WriteFile(AFileName);
}
catch { }
System.Web.HttpContext.Current.Response.Cache.SetExpires(DateTime.Now.AddDays(7));
System.Web.HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public);
System.Web.HttpContext.Current.Response.End();
}
/// <summary>
/// 设置输出类型
/// </summary>
/// <param name="fullfilepath"></param>
protected void SetContentType(string fullfilepath)
{
string extFileName = Path.GetExtension(fullfilepath);
string contentType = "image/jpeg";
switch (extFileName.Trim().ToLower())
{
case ".jpg":
contentType = "image/jpeg";
break;
case ".png":
contentType = "image/png";
break;
case ".gif":
contentType = "image/gif";
break;
case ".bmp":
contentType = "image/bmp";
break;
case ".tif":
contentType = "image/tiff";
break;
}
System.Web.HttpContext.Current.Response.ContentType = contentType;
}
/// <summary>
/// 检查是否存在该尺寸的缩略图
/// </summary>
/// <param name="itsize"></param>
/// <param name="filepath"></param>
/// <returns></returns>
protected string CheckRequestSizeIsExists(string itsize, string filepath)
{
string _path = Path.Combine(config.webUpPath2, itsize);
_path = Path.Combine(_path, filepath);
if (File.Exists(HttpContext.Current.Server.MapPath(_path)))
{
return _path;
}
else
{
return "";
}
}
/// <summary>
/// 获取输出的文件路径
/// </summary>
/// <returns></returns>
protected string GetFullFilePathByPath()
{
string _filepath = "";
string ate_FilePath = Query.Get("image", "");
//判断记录是否存在
if (ate_FilePath != "")
{
//_filepath = Path.Combine(HttpContext.Current.Server.MapPath(config.webWatemarkUrl), ate_FilePath);
_filepath = Path.Combine(config.webUpPath, _filepath);
if (!File.Exists(HttpContext.Current.Server.MapPath(_filepath))) //水印图不存在,获取原图
{
_filepath = ate_FilePath;
_filepath = Path.Combine(config.webUpPath, _filepath);
}
//需要缩略图
string itsize = Query.Get("itsize", "");
if (Mtxfw.Utility.Common.Is_ThumbNail_AllowSizes(itsize)) //是否为允许的尺寸
{
//获取已存在的缩略图
string existfilepath = CheckRequestSizeIsExists(itsize, ate_FilePath);
//没有该尺寸已生成好的图,创建缩略图
if (existfilepath == "")
{
int itql = Query.Get("itql", int.Parse(config.webWatemarkQuality));
string[] width_height = itsize.Split('x');
string saveFilePath = Path.Combine(config.webUpPath2, itsize);
saveFilePath = Path.Combine(saveFilePath, ate_FilePath);
string savepath = Path.GetDirectoryName(saveFilePath);
if (!Directory.Exists(HttpContext.Current.Server.MapPath(savepath)))
{
Directory.CreateDirectory(HttpContext.Current.Server.MapPath(savepath));
}
string oriFilePath = ate_FilePath;
oriFilePath = Path.Combine(config.webUpPath, oriFilePath);
Mtxfw.Utility.ImageManager.CutForCustom(HttpContext.Current.Server.MapPath(oriFilePath), HttpContext.Current.Server.MapPath(saveFilePath), Int32.Parse(width_height[0]), Int32.Parse(width_height[1]), itql);
return saveFilePath; //返回新生成的缩位图
}
else
{
return existfilepath; //返回已生成的缩位图
}
}
}
return _filepath; //返回原始图片
}
public bool IsReusable{get{return false;}}
}
}