62,244
社区成员




using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Services.Description;
/// <summary>
///fileupload 的摘要说明
/// </summary>
public class fileupload
{
private int FileSize;//默认文件大小
private string FilePath;//默认文件路径
private string FileName;//默认文件名字
private string[] FileFormat;//默认文件格式
private string ErrorMsg;//默认错误信息
private string[] FileType;//默认文件MINE类型
private int UploadFileSize;
private string UploadFileName;
private string UploadFilePath;
public fileupload()
{
FileSize = 20480;//2mb
FilePath = @"C:\Inetpub\wwwroot\upload\";
FileName = Regex.Replace(DateTime.Now.ToString(), "( |:|-|上午|下午)", "");
FileFormat = new string[] { "jpg", "jpeg", "gif", "png","bmp"};
FileType = new string[] { "image" };
ErrorMsg = null;
}
public string Msg
{
get
{
return ErrorMsg;
}
set
{
ErrorMsg = value;
}
}
public int GetSize
{
get
{
return UploadFileSize;
}
}
public string GetName
{
get
{
return UploadFileName;
}
}
public string GetPath
{
get
{
return UploadFilePath;
}
}
public fileupload(int size, string path, string[] format,string[] type)
{
FileSize = size;
FilePath = path;
FileFormat = format;
FileType = type;
}
public fileupload(string path)
{
FilePath = path;
}
public bool FileUpload(FileUpload fu)
{
string CurrentFileName = fu.FileName.Trim();//取得当前文件名字
string CurrentFullName = fu.PostedFile.FileName.Trim();//取得当前文件整个名字
int CurrentFileSize = fu.PostedFile.ContentLength;//取得当前文件大小
string CurrentFileType = fu.PostedFile.ContentType.Trim();//取得当前文件MINE类型
StringBuilder sb=new StringBuilder();//返回字符串式的文件类型
int ffl = FileFormat.Length;//文件格式的数组长度
int fnl = CurrentFileName.Split('.').Length;//返回文件名数组的长度
string[] cfns=CurrentFileName.Split('.');//返回文件名字的数组
int ftl=FileType.Length;//返回Mine数组的长度
StringBuilder sb1=new StringBuilder();//返回Mine数组字符串
for(int i=0;i<ftl;i++)
{
if(i< ftl - 1)
{
sb1.Append(FileType[i].ToString().Trim()+"|");
}
else
{
sb1.Append(FileType[i].ToString().Trim());
}
}
for (int i = 0; i < ffl; i++)
{
if (i < ffl - 1)
{
sb.Append(FileFormat[i].ToString().Trim()+"|");
}
else
{
sb.Append(FileFormat[i].ToString().Trim());
}
}
Regex reg = new Regex(@"^[a-z]\:\\([^""\.\\\<\>\:\/\?\|][^""\\\<\>\:\/\?\|]+[^""\.\\\<\>\:\/\?\|]\\)*[^""\\\<\>\:\/\?\|]+\.(" + sb.ToString() + ")$", RegexOptions.IgnoreCase);
try
{
if (fnl < 2)
{
Msg = "你上传的文件名出现错误。<br>";
return false;
}
else
{
if (sb.ToString().IndexOf(cfns[fnl-1].ToString().Trim().ToLower()) >= 0 || sb.ToString().IndexOf(cfns[fnl-1].ToString().Trim().ToUpper()) >= 0)
{
if (!reg.Match(CurrentFullName).Success)
{
Msg = Msg + "你上传的文件路径出现错误。<br>";
return false;
}
else
{
if (CurrentFileSize > FileSize)
{
Msg = Msg + "上传文件超过系统默认大小," + "当前文件大小:" + (float)(CurrentFileSize/1024)+ "KB,系统默认大小:" + (float)(FileSize/1024)+"KB。<br>";
return false;
}
else
{
if (CurrentFileType.Split('/').Length>0)
{
if (sb1.ToString().IndexOf(CurrentFileType.Split('/')[0].ToString().Trim().ToLower()) >= 0)
{
string filename = FileName + "." + cfns[1];
UploadFileSize = CurrentFileSize;
UploadFileName = filename;
UploadFilePath = FilePath;
fu.PostedFile.SaveAs(FilePath + filename);
return true;
}
else
{
Msg = Msg + "上传文件MIME类型错误,你的MIME类型为:"+CurrentFileType+"。<br>";
return false;
}
}
else
{
Msg = Msg + "上传文件MIME类型错误,你的MIME类型为:" + CurrentFileType + "。<br>";
return false;
}
}
}
}
else
{
Msg = "文件类型不正确,不能为:“" + cfns[fnl - 1] + "”的后缀名,系统默认文件类型为:" + sb.ToString().Replace('|', ',') + "。<br>";
return false;
}
}
}
catch (Exception ex)
{
Msg = Msg+ex.Message;
return false;
}
}
}
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Globalization;
using System.IO;
using System.Text;
using System.Web;
using System.Reflection;
public class HttpUploadModule : IHttpModule
{
private HttpApplication hApp;
public HttpUploadModule()
{
}
public int nState;
public void Init(HttpApplication application)
{
hApp = application;
application.BeginRequest += new EventHandler(this.Application_BeginRequest);
application.EndRequest += new EventHandler(this.Application_EndRequest);
application.Error += new EventHandler(this.Application_Error);
nState = 1;
}
public void Dispose()
{
}
private void Application_BeginRequest(Object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
HttpWorkerRequest request = GetWorkerRequest(app.Context);
Encoding encoding = app.Context.Request.ContentEncoding;
int bytesRead = 0; // 已读数据大小
int read; // 当前读取的块的大小
int count = 8192; // 分块大小
byte[] buffer; // 保存所有上传的数据
string uploadId; // 唯一标志当前上传的ID
Progress progress; // 记录当前上传的进度信息
if (request != null)
{
// 返回 HTTP 请求正文已被读取的部分。
//
byte[] tempBuff = request.GetPreloadedEntityBody();
// 如果是附件上传
//
if (tempBuff != null && IsUploadRequest(app.Request))
{
// 当前上传的ID,用来唯一标志当前的上传
// 用此UploadID,可以通过其他页面获取当前上传的进度
if (app.Context.Request.Cookies["UploadID"] == null)
{
HttpCookie hCookie = new HttpCookie("UploadID");
hCookie.Value = Guid.NewGuid().ToString();
app.Context.Response.Cookies.Add(hCookie);
}
uploadId = app.Context.Request.Cookies["UploadID"].Value.ToString();
long length = long.Parse(request.GetKnownRequestHeader(HttpWorkerRequest.HeaderContentLength));
//判断文件大小,超出了就不传
System.Xml.XmlDocument xml = new System.Xml.XmlDocument();
xml.Load(app.Context.Server.MapPath("~/web.config"));
System.Xml.XmlNode node = xml.SelectSingleNode("configuration/system.web/httpRuntime");
long nMaxLength = Convert.ToInt64(node.Attributes["maxRequestLength"].Value) * 1024;
if (length > nMaxLength)
{
Fish.common.MessageBox("上传文件超出最大值,系统最大允许" + Fish.common.ConvertByte(nMaxLength), false);
nState = -1;
Application_EndRequest(sender, e);
Fish.common.GoUrl(app.Context.Request.ServerVariables["http_referer"]);
}
// 获取上传大小
//
//将数据保存到文件里
using(FileStream fs = File.Open(app.Context.Server.MapPath(uploadId + ".tmp"), FileMode.OpenOrCreate))
{
// 将已上传数据复制过去
fs.Write(tempBuff, 0, tempBuff.Length);
// 开始记录当前上传状态
//
progress = new Progress(length, uploadId);
progress.SetState(UploadState.ReceivingData);
//buffer = new byte[length];
count = tempBuff.Length; // 分块大小
// 开始记录已上传大小
//
bytesRead = tempBuff.Length;
progress.SetBytesRead(bytesRead);
SetProgress(uploadId, progress, app.Application);
// 循环分块读取,直到所有数据读取结束
//
//测试 恢复count
//count = 1;
while (request.IsClientConnected() && !request.IsEntireEntityBodyIsPreloaded() && bytesRead < length)
{
// 如果最后一块大小小于分块大小,则重新分块
//
if (bytesRead + count > length)
{
count = (int)(length - bytesRead);
tempBuff = new byte[count];
}
// 分块读取
//
read = request.ReadEntityBody(tempBuff, count);
// 复制已读数据块
//
//----Buffer.BlockCopy(tempBuff, 0, buffer, bytesRead, read);
fs.Write(tempBuff, 0, read);
// 记录已上传大小
//
bytesRead += read;
progress.SetBytesRead(bytesRead);
SetProgress(uploadId, progress, app.Application);
}
if (request.IsClientConnected() && !request.IsEntireEntityBodyIsPreloaded())
{
// 传入已上传完的数据
//
fs.Close();
fs.Dispose();
FileStream fRead = File.Open(app.Context.Server.MapPath(uploadId + ".tmp"), FileMode.Open);
buffer = new byte[fRead.Length];
int nLen = fRead.Read(buffer, 0, (int)fRead.Length);
fRead.Close();
fRead.Dispose();
InjectTextParts(request, buffer);
buffer = null;
// 表示上传已结束
//
progress.SetBytesRead(bytesRead);
progress.SetState(UploadState.Complete);
SetProgress(uploadId, progress, app.Application);
}
//删除文件
tempBuff = null;
fs.Close();
fs.Dispose();
}
File.Delete(app.Context.Server.MapPath(uploadId + ".tmp"));
}
}
}
/// 结束请求后移除进度信息
private void Application_EndRequest(Object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
if (IsUploadRequest(app.Request) && nState > 0)
{
SetUploadState(app, UploadState.Complete);
RemoveFrom(app);
}
}
/// 如果出错了设置进度信息中状态为“Error”
private void Application_Error(Object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
if (IsUploadRequest(app.Request) && nState > 0)
{
SetUploadState(app, UploadState.Error);
}
}
/// 设置当前上传进度信息的状态
void SetUploadState(HttpApplication app, UploadState state)
{
string uploadId = app.Context.Request.Cookies["UploadID"].ToString();
if (uploadId != null && uploadId.Length > 0)
{
Progress progress = GetProgress(uploadId, app.Application);
if (progress != null)
{
progress.SetState(state);
SetProgress(uploadId, progress, app.Application);
}
}
}
/// 设置当前上传的进度信息
/// 根据UploadID记录在Application中
void SetProgress(string uploadId, Progress progress, HttpApplicationState application)
{
if (uploadId == null || uploadId == string.Empty || progress == null)
return;
application.Lock();
application["Fish_" + uploadId] = progress;
application.UnLock();
}
/// <summary>
/// 从Application中移出进度信息
/// </summary>
/// <param name="app"></param>
void RemoveFrom(HttpApplication app)
{
string uploadId = app.Context.Request.Cookies["UploadID"].Value.ToString();
HttpApplicationState application = app.Application;
if (uploadId != null && uploadId.Length > 0)
{
application.Remove("Fish_" + uploadId);
app.Context.Request.Cookies.Remove("UploadID");
}
}
/// 根据UploadID获取上传进度信息
public static Progress GetProgress(string uploadId, HttpApplicationState application)
{
Progress progress = application["Fish_" + uploadId] as Progress;
return progress;
}
HttpWorkerRequest GetWorkerRequest(HttpContext context)
{
IServiceProvider provider = (IServiceProvider)HttpContext.Current;
return (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));
}
/// 传入已上传完的数据
void InjectTextParts(HttpWorkerRequest request, byte[] textParts)
{
BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic;
Type type = request.GetType();
while ((type != null) && (type.FullName != "System.Web.Hosting.ISAPIWorkerRequest"))
{
type = type.BaseType;
}
if (type != null)
{
type.GetField("_contentAvailLength", bindingFlags).SetValue(request, textParts.Length);
type.GetField("_contentTotalLength", bindingFlags).SetValue(request, textParts.Length);
type.GetField("_preloadedContent", bindingFlags).SetValue(request, textParts);
type.GetField("_preloadedContentRead", bindingFlags).SetValue(request, true);
}
}
private static bool StringStartsWithAnotherIgnoreCase(string s1, string s2)
{
return (string.Compare(s1, 0, s2, 0, s2.Length, true, CultureInfo.InvariantCulture) == 0);
}
/// 是否为附件上传
bool IsUploadRequest(HttpRequest request)
{
return StringStartsWithAnotherIgnoreCase(request.ContentType, "multipart/form-data");
}
}
//添加附件控件
function addFile(addNew)
{
var trAtt = document.getElementById("trAtt");
trAtt.style.display = "";
if(addNew)
{
var tdAttachment = document.getElementById("attachmentId");
if(tdAttachment)
{
var oDiv = document.createElement('div');
tdAttachment.appendChild(oDiv);
var oFile = document.createElement('<input type="file" size="50" name="File" runat="server" >');
oDiv.appendChild(oFile);
var oButton = document.createElement('<button onclick="removeme()">');
oButton.innerHTML = '删除';
oDiv.appendChild(oButton);
// document.getElementById("appendLink").alt = '继续添加附件';
}
}
}
/// <summary>
/// 保存新加附件
/// </summary>
/// <returns></returns>
private void SaveAttachment()
{
try
{
string strPath = ConfigurationManager.AppSettings["UploadFilesPath"];
string RootPath = Server.MapPath(strPath + "/CorpRules/" + User.Identity.Name + "/");
string virtualPath = "/CorpRules/" + User.Identity.Name + "/";
if (!Directory.Exists(RootPath))
{
Directory.CreateDirectory(RootPath);
}
HttpFileCollection fileList = HttpContext.Current.Request.Files;
for (int i = 0; i < fileList.Count; i++)
{
HttpPostedFile file = fileList[i];
string fileN = file.FileName.Substring(file.FileName.LastIndexOf("\\") + 1);
string fileName = RootPath + "\\" + fileN;
if (file.ContentLength > 0)
{
fileName = CheckFileName(fileName);
file.SaveAs(fileName);
int fileSize = file.ContentLength / 1024;
CorpRulesAttachment entTmp = (CorpRulesAttachment)Weith.Toolkit.Reflection.TypeAccessor.CreateInstance(typeof(CorpRulesAttachment));
entTmp.CorpRulesAttachmentID = Guid.NewGuid();
entTmp.Path = virtualPath + fileN;
entTmp.Size = fileSize;
attList.Add(entTmp);
}
}
}
catch (Exception)
{
DelFiles();
Response.Redirect("~\\Error\\ServerError.aspx");
}
}