using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using CY.Config;
using System.IO;
using CY.Infrastructure;
using Microsoft.Win32;
using System.Diagnostics;
namespace CY.WebForm.cs
{
public class UploadCS
{
#region 文件上传
///
/// 上传返回结果类
///
public class UpFileResult
{
private ArrayList _returnfilename = new ArrayList();
///
/// 保存的文件路径名
///
public ArrayList returnfilename
{
get
{
return _returnfilename;
}
set
{
_returnfilename = value;
}
}
private ArrayList _returnerror = new ArrayList();
///
/// 保存失败的描述信息
///
public ArrayList returnerror
{
get
{
return _returnerror;
}
set
{
_returnerror = value;
}
}
private bool _isEmpty = false;
///
/// 上传文件是否为空
///
public bool isEmpty
{
get
{
return _isEmpty;
}
set
{
_isEmpty = value;
}
}
}
///
/// 上传文件
///
/// 上传控件ID
/// 原文件路径
///
public static UpFileResult Upload(string input_name, string old_path)
{
string path = "/images/Upload/" + DateTime.Now.ToString("yyyyMMdd") + "/";
return Upload(input_name, path, old_path);
}
///
/// 上传文件
///
/// 上传控件ID
/// 保存路径
/// 原文件路径
///
public static UpFileResult Upload(string input_name, string folder_path, string old_path)
{
////保存的文件路径名
//ArrayList returnfilename = new ArrayList();
////保存失败的描述信息
//ArrayList returnerror = new ArrayList();
////上传文件是否为空
//bool isEmpty = false;
UpFileResult _UpFileResult = new UpFileResult();
string WebDomain = "http://" + WebInfo.Instance.WebDomain.ToString().Trim('/').Replace("http://", "");
string UploadFileSize = WebInfo.Instance.UploadFileSize.ToString() ?? "0";
string UploadFileType = WebInfo.Instance.UploadFileType.ToString() ?? "";
try
{
List fileList = new List();
if (string.IsNullOrEmpty(input_name))
{
for (int i = 0; i < HttpContext.Current.Request.Files.Count; i++)
fileList.Add(HttpContext.Current.Request.Files[i]);
}
else
{
for (int i = 0; i < HttpContext.Current.Request.Files.Keys.Count; i++)
{
if (input_name == HttpContext.Current.Request.Files.Keys[i].ToString())
fileList.Add(HttpContext.Current.Request.Files[i]);
}
}
foreach (HttpPostedFile file in fileList)
{
if (file == null || file.ContentLength == 0)
{
_UpFileResult.isEmpty = true;
}
else if (Convert.ToDouble(file.ContentLength) > Convert.ToDouble(UploadFileSize) * 1024)
_UpFileResult.returnerror.Add(string.Format("文件不能大于 {0}K", UploadFileSize));
else
{
//当前格式
string Extension = System.IO.Path.GetExtension(file.FileName).Replace(".", "");
//判断格式
bool isExtensionRight = false;
string[] Extensions = UploadFileType.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < Extensions.Length; i++)
{
if (Extensions[i].Trim().ToLower() == Extension.ToLower())
{
isExtensionRight = true;
break;
}
}
//格式正确
if (isExtensionRight)
{
string filename = DateTime.Now.ToString("yyyyMMddHHmmssffff") + MathRandom.RandomNumber(4) + "fl." + Extension;
filename = "t" + filename.Replace("/", "");
string AllFolderPath = HttpContext.Current.Server.MapPath("~" + folder_path);
if (!Directory.Exists(AllFolderPath))
Directory.CreateDirectory(AllFolderPath);
//删除原上传图片
if (!string.IsNullOrEmpty(old_path) && File.Exists(HttpContext.Current.Server.MapPath("~" + old_path.Replace(WebDomain, ""))))
{
//File.Delete(HttpContext.Current.Server.MapPath("~" + old_path.Replace(WebDomain, "")));
}
// 保存文件
file.SaveAs(HttpContext.Current.Server.MapPath("~" + folder_path + filename));
System.Threading.Thread.Sleep(10);
_UpFileResult.returnfilename.Add(WebDomain + folder_path + filename);
}
else
_UpFileResult.returnerror.Add(string.Format("格式不在允许的范围内,只允许格式为:{0}", UploadFileType));
}
}
}
catch
{
_UpFileResult.returnerror.Add("上传文件出错");
}
return _UpFileResult;
}
///
/// 上传软件版本文件需要压缩包RAR
///
/// 上传控件ID
/// 版本号
///
public static UpFileResult UploadSoftVersion(string input_name, string VerString,string SoftCode)
{
////保存的文件路径名
//ArrayList returnfilename = new ArrayList();
////保存失败的描述信息
//ArrayList returnerror = new ArrayList();
////上传文件是否为空
//bool isEmpty = false;
UpFileResult _UpFileResult = new UpFileResult();
string WebDomain = "http://soft.cyin.cn";
string UploadFileSize = "100";
string UploadFileType = "rar";
try
{
List fileList = new List();
if (string.IsNullOrEmpty(input_name))
{
for (int i = 0; i < HttpContext.Current.Request.Files.Count; i++)
fileList.Add(HttpContext.Current.Request.Files[i]);
}
else
{
for (int i = 0; i < HttpContext.Current.Request.Files.Keys.Count; i++)
{
if (input_name == HttpContext.Current.Request.Files.Keys[i].ToString())
fileList.Add(HttpContext.Current.Request.Files[i]);
}
}
foreach (HttpPostedFile file in fileList)
{
if (file == null || file.ContentLength == 0)
{
_UpFileResult.isEmpty = true;
}
else if (Convert.ToDouble(file.ContentLength) > Convert.ToDouble(UploadFileSize) * 1024 * 1024)
_UpFileResult.returnerror.Add(string.Format("文件不能大于 {0} M", UploadFileSize));
else
{
//当前格式
string Extension = System.IO.Path.GetExtension(file.FileName).Replace(".", "");
//判断格式
bool isExtensionRight = false;
string[] Extensions = UploadFileType.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < Extensions.Length; i++)
{
if (Extensions[i].Trim().ToLower() == Extension.ToLower())
{
isExtensionRight = true;
break;
}
}
//格式正确
if (isExtensionRight)
{
string filename = DateTime.Now.ToString("yyyyMMddHHmmssffff") + MathRandom.RandomNumber(4) + "fl." + Extension;
filename = "t" + filename.Replace("/", "");
string AllFolderPath = "E:\\DocumentService\\" + SoftCode + "\\";
if (!Directory.Exists(AllFolderPath))
Directory.CreateDirectory(AllFolderPath);
//保存文件
file.SaveAs(AllFolderPath + "\\" + filename);
System.Threading.Thread.Sleep(10);
UnRAR(AllFolderPath, AllFolderPath, filename);
System.Threading.Thread.Sleep(10);
try
{
File.Delete(AllFolderPath + "\\" + filename);
}
catch (Exception ex)
{
throw ex;
}
_UpFileResult.returnfilename.Add(WebDomain + "/" + VerString + "/" + filename);
}
else
_UpFileResult.returnerror.Add(string.Format("格式不在允许的范围内,只允许格式为:{0}", UploadFileType));
}
}
}
catch
{
_UpFileResult.returnerror.Add("上传文件出错");
}
return _UpFileResult;
}
///
/// 利用 WinRAR 进行解压缩
///
/// 文件解压路径(绝对)
/// 将要解压缩的 .rar 文件的存放目录(绝对路径)
/// 将要解压缩的 .rar 文件名(包括后缀)
/// true 或 false。解压缩成功返回 true,反之,false。
public static bool UnRAR(string path, string rarPath, string rarName)
{
bool flag = false;
string rarexe;
RegistryKey regkey;
Object regvalue;
string cmd;
ProcessStartInfo startinfo;
Process process;
try
{
regkey = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\shell\open\command");
regvalue = regkey.GetValue("");
rarexe = regvalue.ToString();
regkey.Close();
rarexe = rarexe.Substring(1, rarexe.Length - 7);
Directory.CreateDirectory(path);
//解压缩命令,相当于在要压缩文件(rarName)上点右键 ->WinRAR->解压到当前文件夹
cmd = string.Format("x {0} {1} -y", rarName, path);
startinfo = new ProcessStartInfo();
startinfo.FileName = rarexe;
startinfo.Arguments = cmd;
startinfo.WindowStyle = ProcessWindowStyle.Hidden;
startinfo.WorkingDirectory = rarPath;
process = new Process();
process.StartInfo = startinfo;
process.Start();
process.WaitForExit();
if (process.HasExited)
{
flag = true;
}
process.Close();
}
catch (Exception e)
{
throw e;
}
return flag;
}
#endregion
#region 获取服务端和物理目录地址
///
/// 获取服务端地址
///
///
///
public static string GetRootUrl(string forumPath)
{
forumPath = forumPath.IndexOf("/") == 0 ? forumPath.Substring(1) : forumPath;
string ApplicationPath = HttpContext.Current.Request.ApplicationPath != "/" ? HttpContext.Current.Request.ApplicationPath : string.Empty;
int port = HttpContext.Current.Request.Url.Port;
return string.Format("{0}://{1}{2}{3}/{4}",
HttpContext.Current.Request.Url.Scheme,
HttpContext.Current.Request.Url.Host,
(port == 80 || port == 0) ? "" : ":" + port,
ApplicationPath,
forumPath);
}
///
/// 获取物理目录地址
///
///
///
public static string GetMapPath(string strPath)
{
if (HttpContext.Current != null)
{
return HttpContext.Current.Server.MapPath(strPath);
}
else //非web程序引用
{
strPath = strPath.Replace("/", "\\");
if (strPath.StartsWith("\\"))
{
strPath = strPath.Substring(strPath.IndexOf('\\', 1)).TrimStart('\\');
}
return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
}
}
#endregion
}
}