using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Net;
using System.Text;
using Itlead.Architecture.WebUI.Common;
namespace Itlead.Architecture.WebUI.tools
{
///
/// VUpload 的摘要说明
///
public class VUpload : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
//context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
context.Response.Expires = 0;
context.Response.CacheControl = "no-cache";
context.Response.AddHeader("Pragma", "No-Cache");
string _fileNamePath = "";
try
{
_fileNamePath = context.Request.Files[0].FileName;
//开始上传
UpLoadVideo(_fileNamePath, context);
}
catch
{
context.Response.Write("{msg: 0, msgbox: \"上传过程中发生意外错误!\"}");
}
}
public void UpLoadVideo(string fileNamePath, HttpContext context)
{
try
{
string serverPath = System.Web.HttpContext.Current.Server.MapPath("~");
string toFilePath = Path.Combine(serverPath, "Upload/");
//获取要保存的文件信息
FileInfo file = new FileInfo(fileNamePath);
//获得文件扩展名
string fileNameExt = file.Extension;
//验证合法的文件
if (CheckImageExt(fileNameExt))
{
//生成将要保存的随机文件名
string fileName = GetImageName() + fileNameExt;
//获得要保存的文件路径
string NewFilePaht = DateTime.Now.ToString("yyyyMM") + "/" + DateTime.Now.ToString("dd");
string serverFileName = toFilePath + NewFilePaht;
//物理完整路径
if (!Directory.Exists(serverFileName))
{
Directory.CreateDirectory(serverFileName);
}
string toFileFullPath = serverFileName + "/" + fileName; //HttpContext.Current.Server.MapPath(toFilePath);
//将要保存的完整文件名
string toFile = toFileFullPath;//+ fileName;
///创建WebClient实例
WebClient myWebClient = new WebClient();
//设定windows网络安全认证 方法1
myWebClient.Credentials = CredentialCache.DefaultCredentials;
////设定windows网络安全认证 方法2
context.Request.Files[0].SaveAs(toFile);
//上传成功后网站内源图片相对路径
string relativePath = System.Web.HttpContext.Current.Request.ApplicationPath
+ string.Format(@"Upload/" + NewFilePaht + "/{0}", fileName);
//返回原图和微缩图的网站相对路径
context.Response.Write("{msg:1, imgUrl: \"" + relativePath + "\"}");
}
else
{
// return "文件格式非法,请上传gif或jpg格式的文件。";
//throw new Exception("文件格式非法,请上传gif或jpg格式的文件。");
context.Response.Write("{msg: 0, msgbox: \"文件格式非法!\"}");
}
}
catch
{
// return ex.Message;
context.Response.Write("{msg: 0, msgbox: \"上传过程中发生意外错误!\"}");
}
}
#region Private Methods
///
/// 检查是否为合法的上传图片
///
///
///
private bool CheckImageExt(string imageExt)
{
string[] allowExt = new string[] { ".flv", ".rm", ".rmv", ".pdf", ".doc", ".xls",".txt",".rar",".zip" };
//for (int i = 0; i < allowExt.Length; i++)
//{
// if (allowExt[i] == _ImageExt) { return true; }
//}
StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
return allowExt.Any(c => stringComparer.Equals(c, imageExt));
}
private string GetImageName()
{
Random rd = new Random();
StringBuilder serial = new StringBuilder();
serial.Append(DateTime.Now.ToString("yyyyMMddHHmmssff"));
serial.Append(rd.Next(0, 999999).ToString());
return serial.ToString();
}
public int DefaultHeight
{
get
{
//此处硬编码了,可以写入配置文件中。
return 150;
}
}
#endregion
public bool IsReusable
{
get
{
return false;
}
}
}
}