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 文件上传
|
|
/// <summary>
|
/// 上传返回结果类
|
/// </summary>
|
public class UpFileResult
|
{
|
private ArrayList _returnfilename = new ArrayList();
|
/// <summary>
|
/// 保存的文件路径名
|
/// </summary>
|
public ArrayList returnfilename
|
{
|
get
|
{
|
return _returnfilename;
|
}
|
set
|
{
|
_returnfilename = value;
|
}
|
}
|
|
private ArrayList _returnerror = new ArrayList();
|
/// <summary>
|
/// 保存失败的描述信息
|
/// </summary>
|
public ArrayList returnerror
|
{
|
get
|
{
|
return _returnerror;
|
}
|
set
|
{
|
_returnerror = value;
|
}
|
}
|
|
private bool _isEmpty = false;
|
/// <summary>
|
/// 上传文件是否为空
|
/// </summary>
|
public bool isEmpty
|
{
|
get
|
{
|
return _isEmpty;
|
}
|
set
|
{
|
_isEmpty = value;
|
}
|
}
|
}
|
|
/// <summary>
|
/// 上传文件
|
/// </summary>
|
/// <param name="input_name">上传控件ID</param>
|
/// <param name="old_path">原文件路径</param>
|
/// <returns></returns>
|
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);
|
}
|
|
/// <summary>
|
/// 上传文件
|
/// </summary>
|
/// <param name="input_name">上传控件ID</param>
|
/// <param name="folder_path">保存路径</param>
|
/// <param name="old_path">原文件路径</param>
|
/// <returns></returns>
|
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<HttpPostedFile> fileList = new List<HttpPostedFile>();
|
|
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;
|
}
|
|
/// <summary>
|
/// 上传软件版本文件需要压缩包RAR
|
/// </summary>
|
/// <param name="input_name">上传控件ID</param>
|
/// <param name="VerString">版本号</param>
|
/// <returns></returns>
|
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<HttpPostedFile> fileList = new List<HttpPostedFile>();
|
|
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;
|
}
|
|
/// <summary>
|
/// 利用 WinRAR 进行解压缩
|
/// </summary>
|
/// <param name="path">文件解压路径(绝对)</param>
|
/// <param name="rarPath">将要解压缩的 .rar 文件的存放目录(绝对路径)</param>
|
/// <param name="rarName">将要解压缩的 .rar 文件名(包括后缀)</param>
|
/// <returns>true 或 false。解压缩成功返回 true,反之,false。</returns>
|
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 获取服务端和物理目录地址
|
|
/// <summary>
|
/// 获取服务端地址
|
/// </summary>
|
/// <param name="forumPath"></param>
|
/// <returns></returns>
|
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);
|
}
|
|
/// <summary>
|
/// 获取物理目录地址
|
/// </summary>
|
/// <param name="strPath"></param>
|
/// <returns></returns>
|
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
|
}
|
}
|