135 lines
5.8 KiB
C#
135 lines
5.8 KiB
C#
using Ninject;
|
||
using System;
|
||
using System.IO;
|
||
using System.Web;
|
||
using WX.CRM.Common;
|
||
using WX.CRM.DAL.Redis;
|
||
using WX.CRM.IBLL.Wx;
|
||
using WX.CRM.WebHelper.Infrastructure;
|
||
namespace WxService
|
||
{
|
||
/// <summary>
|
||
/// WxFileUpload2 的摘要说明
|
||
/// </summary>
|
||
public class FilePath2 : IHttpHandler
|
||
{
|
||
private static string _shareName;
|
||
private static string _isShare;
|
||
private IWx_MsgKey msgkey = NinjectControllerFactory.ninjectKernel.Get<IWx_MsgKey>();
|
||
static FilePath2()
|
||
{
|
||
//用户名勿比指定共享服务器的IP或名称,否则会引起1312错误
|
||
_isShare = Utility.GetSettingByKey("IsShare");
|
||
if (!string.IsNullOrEmpty(_isShare))
|
||
{
|
||
_shareName = Utility.GetSettingByKey("NetUseShareName") ?? @"\\192.168.1.171\weixin";
|
||
string user = Utility.GetSettingByKey("NetUseUser") ?? @"192.168.1.171\admin";
|
||
string pwd = Utility.GetSettingByKey("NetUsePwd") ?? "read,./1";
|
||
NetUseHelper.Build(_shareName, user, pwd, string.Empty);//不指定盘符,避免引起盘符被占用的错误
|
||
}
|
||
}
|
||
|
||
public void ProcessRequest(HttpContext context)
|
||
{
|
||
try
|
||
{
|
||
context.Response.ContentType = "text/plain";
|
||
var msgSvrId = context.Request.Params["msgSvrId"];
|
||
var msgType = context.Request.Params["msgType"];
|
||
|
||
string keyname = "wx:" + msgSvrId;
|
||
var redis = new RedisString<string>(keyname);
|
||
//存放在数据库中的路径
|
||
var filePath = redis.Get();
|
||
if (string.IsNullOrEmpty(filePath))
|
||
{
|
||
filePath = msgkey.GetFile(keyname);//如果redis查找不到,就从数据库中查找一次
|
||
}
|
||
//没有找到路径
|
||
if (string.IsNullOrEmpty(filePath))
|
||
{
|
||
if (msgType == "3" || msgType == "47")
|
||
{
|
||
//byte[] buffurPic = Convert.FromBase64String("data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==");
|
||
var buffurPic = Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==");
|
||
context.Response.Clear();
|
||
context.Response.ContentType = "image/gif";
|
||
context.Response.BufferOutput = true;
|
||
context.Response.OutputStream.Write(buffurPic, 0, buffurPic.Length);
|
||
context.Response.Flush();
|
||
}
|
||
return;
|
||
}
|
||
|
||
//存放在共享目录中的路径
|
||
var shareFilePath = _shareName + @"\WXFILE" + filePath;
|
||
var fileName = Path.GetFileName(filePath);
|
||
if (msgType == "3" || msgType == "47")
|
||
{
|
||
//图片处理
|
||
var fs = new FileStream(shareFilePath, FileMode.Open, FileAccess.Read);
|
||
//var fs = new FileStream(@"\\192.168.1.171\weixin\uploadfile\20181\775b1ae28ef3c43f1a8ad02fe0cfd0c0.jpg", FileMode.Open, FileAccess.Read);
|
||
byte[] buffur = new byte[fs.Length];
|
||
fs.Read(buffur, 0, (int)fs.Length);
|
||
string strPic = Convert.ToBase64String(buffur);
|
||
|
||
byte[] buffurPic = Convert.FromBase64String(strPic);
|
||
|
||
context.Response.Clear();
|
||
context.Response.ContentType = "image/jpeg";
|
||
context.Response.BufferOutput = true;
|
||
context.Response.OutputStream.Write(buffurPic, 0, buffurPic.Length);
|
||
context.Response.Flush();
|
||
}
|
||
else if (msgType == "34")
|
||
{
|
||
//语音处理
|
||
var fileNameWithoutExt = Path.GetFileNameWithoutExtension(filePath);
|
||
shareFilePath = _shareName + @"\WXFILE\uploadfile\audio\" + fileNameWithoutExt + ".mp3";
|
||
FileStream fs = new FileStream(shareFilePath, FileMode.Open, FileAccess.Read);
|
||
byte[] buffur = new byte[fs.Length];
|
||
fs.Read(buffur, 0, (int)fs.Length);
|
||
string strPic = Convert.ToBase64String(buffur);
|
||
|
||
byte[] buffurPic = Convert.FromBase64String(strPic);
|
||
|
||
context.Response.Clear();
|
||
context.Response.ContentType = "audio/mp3";
|
||
context.Response.BufferOutput = true;
|
||
context.Response.OutputStream.Write(buffurPic, 0, buffurPic.Length);
|
||
context.Response.Flush();
|
||
}
|
||
else if (msgType == "43" || msgType == "49")
|
||
{
|
||
//视频、办公文件,直接下载
|
||
byte[] buffurPic = File.ReadAllBytes(shareFilePath);
|
||
|
||
context.Response.Clear();
|
||
context.Response.ContentType = "application/octet-stream";
|
||
context.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName + ";");
|
||
context.Response.BufferOutput = true;
|
||
context.Response.OutputStream.Write(buffurPic, 0, buffurPic.Length);
|
||
context.Response.Flush();
|
||
}
|
||
|
||
HttpContext.Current.Response.Write(string.Empty);
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Error(ex.ToString());
|
||
HttpContext.Current.Response.Write(string.Empty);
|
||
}
|
||
}
|
||
|
||
public bool IsReusable
|
||
{
|
||
get
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|