上传文件类
轉自:http://www.netfocus.cn/peoplearticle958.html
using System;
using System.IO;
using System.Web;
using System.Web.UI.HtmlControls;
namespace Common
{
public class Upfile
{
private string fileType = null;
private string fName = null;
private string path = null;
private int sizes = 0;
public Upfile()
{
this.path = @"\UpLoadImages\";
this.fileType = "jpg|gif|bmp";
this.sizes = 3136630;
}
public string fileSaveAs(System.Web.UI.WebControls.FileUpload name, bool creatDirectory)
{
try
{
string tFileType;
bool flag;
string filePath = null;
string modifyFileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
string uploadFilePath = null;
if (creatDirectory)
{
uploadFilePath = HttpContext.Current.Server.MapPath("~") + this.path + DateTime.Now.Year.ToString() +"-"+ DateTime.Now.Month.ToString() +"-"+ DateTime.Now.Day.ToString() + @"\";
}
else
{
uploadFilePath = HttpContext.Current.Server.MapPath("~") + this.path;
}
string sourcePath = name.PostedFile.FileName;
switch (sourcePath)
{
case "":
case null:
return null;
default:
{
tFileType = sourcePath.Substring(sourcePath.LastIndexOf(".") + 1).ToLower();
long strLen = name.PostedFile.ContentLength;
string[] temp = this.fileType.Split(new char[] { '|' });
flag = false;
if (strLen >= this.sizes)
{
JScript.MsgBox("上传的档案不能大于" + this.sizes + "KB");
return null;
}
foreach (string data in temp)
{
if (data == tFileType)
{
flag = true;
break;
}
}
break;
}
}
if (!flag)
{
JScript.MsgBox("目前本系统支持的格式为:" + this.fileType);
//this.message("目前本系统支持的格式为:" + this.fileType);
return null;
}
DirectoryInfo dir = new DirectoryInfo(uploadFilePath);
if (!dir.Exists)
{
dir.Create();
}
filePath = uploadFilePath + modifyFileName + "." + tFileType;
name.SaveAs(filePath);
if (creatDirectory)
{
filePath = this.path + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + @"\" + modifyFileName + "." + tFileType;
}
else
{
filePath = this.path + modifyFileName + "." + tFileType;
}
this.fName = modifyFileName + "." + tFileType;
return filePath;
}
catch
{
// this.message("");
//JScript.
JScript.MsgBox("出现错误,请检查图片格式!");
return null;
}
}
public string fileSaveAs(HtmlInputFile name, bool creatDirectory)
{
try
{
string tFileType;
bool flag;
string filePath = null;
string modifyFileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
string uploadFilePath = null;
if (creatDirectory)
{
uploadFilePath = HttpContext.Current.Server.MapPath("~") + this.path + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + @"\";
}
else
{
uploadFilePath = HttpContext.Current.Server.MapPath("~") + this.path;
}
string sourcePath = name.Value.Trim();
switch (sourcePath)
{
case "":
case null:
this.message("请选择你要上传的图片!");
return null;
default:
{
tFileType = sourcePath.Substring(sourcePath.LastIndexOf(".") + 1).ToLower();
long strLen = name.PostedFile.ContentLength;
string[] temp = this.fileType.Split(new char[] { '|' });
flag = false;
if (strLen >= this.sizes)
{
this.message("上传的档案不能大于" + this.sizes + "KB");
return null;
}
foreach (string data in temp)
{
if (data == tFileType)
{
flag = true;
break;
}
}
break;
}
}
if (!flag)
{
this.message("目前本系统支持的格式为:" + this.fileType);
return "";
}
DirectoryInfo dir = new DirectoryInfo(uploadFilePath);
if (!dir.Exists)
{
dir.Create();
}
filePath = uploadFilePath + modifyFileName + "." + tFileType;
name.PostedFile.SaveAs(filePath);
if (creatDirectory)
{
filePath = this.path + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + @"\" + modifyFileName + "." + tFileType;
}
else
{
filePath = this.path + modifyFileName + "." + tFileType;
}
this.fName = modifyFileName + "." + tFileType;
return filePath;
}
catch
{
this.message("出现错误,请检查图片格式!");
return null;
}
}
private void message(string msg)
{
HttpContext.Current.Response.Write("<script language=javascript>alert('" + msg + "');</script>");
}
private void message(string msg, string url)
{
HttpContext.Current.Response.Write("<script language=javascript>alert('" + msg + "');window.location='" + url + "'</script>");
}
public string FileType
{
set
{
this.fileType = value;
}
}
public string FName
{
get
{
return this.fName;
}
set
{
this.fName = value;
}
}
public string Path
{
set
{
this.path = @"\" + value + @"\";
}
}
public int Sizes
{
set
{
this.sizes = value * 0x400;
}
}
}
using System.IO;
using System.Web;
using System.Web.UI.HtmlControls;
namespace Common
{
public class Upfile
{
private string fileType = null;
private string fName = null;
private string path = null;
private int sizes = 0;
public Upfile()
{
this.path = @"\UpLoadImages\";
this.fileType = "jpg|gif|bmp";
this.sizes = 3136630;
}
public string fileSaveAs(System.Web.UI.WebControls.FileUpload name, bool creatDirectory)
{
try
{
string tFileType;
bool flag;
string filePath = null;
string modifyFileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
string uploadFilePath = null;
if (creatDirectory)
{
uploadFilePath = HttpContext.Current.Server.MapPath("~") + this.path + DateTime.Now.Year.ToString() +"-"+ DateTime.Now.Month.ToString() +"-"+ DateTime.Now.Day.ToString() + @"\";
}
else
{
uploadFilePath = HttpContext.Current.Server.MapPath("~") + this.path;
}
string sourcePath = name.PostedFile.FileName;
switch (sourcePath)
{
case "":
case null:
return null;
default:
{
tFileType = sourcePath.Substring(sourcePath.LastIndexOf(".") + 1).ToLower();
long strLen = name.PostedFile.ContentLength;
string[] temp = this.fileType.Split(new char[] { '|' });
flag = false;
if (strLen >= this.sizes)
{
JScript.MsgBox("上传的档案不能大于" + this.sizes + "KB");
return null;
}
foreach (string data in temp)
{
if (data == tFileType)
{
flag = true;
break;
}
}
break;
}
}
if (!flag)
{
JScript.MsgBox("目前本系统支持的格式为:" + this.fileType);
//this.message("目前本系统支持的格式为:" + this.fileType);
return null;
}
DirectoryInfo dir = new DirectoryInfo(uploadFilePath);
if (!dir.Exists)
{
dir.Create();
}
filePath = uploadFilePath + modifyFileName + "." + tFileType;
name.SaveAs(filePath);
if (creatDirectory)
{
filePath = this.path + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + @"\" + modifyFileName + "." + tFileType;
}
else
{
filePath = this.path + modifyFileName + "." + tFileType;
}
this.fName = modifyFileName + "." + tFileType;
return filePath;
}
catch
{
// this.message("");
//JScript.
JScript.MsgBox("出现错误,请检查图片格式!");
return null;
}
}
public string fileSaveAs(HtmlInputFile name, bool creatDirectory)
{
try
{
string tFileType;
bool flag;
string filePath = null;
string modifyFileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
string uploadFilePath = null;
if (creatDirectory)
{
uploadFilePath = HttpContext.Current.Server.MapPath("~") + this.path + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + @"\";
}
else
{
uploadFilePath = HttpContext.Current.Server.MapPath("~") + this.path;
}
string sourcePath = name.Value.Trim();
switch (sourcePath)
{
case "":
case null:
this.message("请选择你要上传的图片!");
return null;
default:
{
tFileType = sourcePath.Substring(sourcePath.LastIndexOf(".") + 1).ToLower();
long strLen = name.PostedFile.ContentLength;
string[] temp = this.fileType.Split(new char[] { '|' });
flag = false;
if (strLen >= this.sizes)
{
this.message("上传的档案不能大于" + this.sizes + "KB");
return null;
}
foreach (string data in temp)
{
if (data == tFileType)
{
flag = true;
break;
}
}
break;
}
}
if (!flag)
{
this.message("目前本系统支持的格式为:" + this.fileType);
return "";
}
DirectoryInfo dir = new DirectoryInfo(uploadFilePath);
if (!dir.Exists)
{
dir.Create();
}
filePath = uploadFilePath + modifyFileName + "." + tFileType;
name.PostedFile.SaveAs(filePath);
if (creatDirectory)
{
filePath = this.path + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + @"\" + modifyFileName + "." + tFileType;
}
else
{
filePath = this.path + modifyFileName + "." + tFileType;
}
this.fName = modifyFileName + "." + tFileType;
return filePath;
}
catch
{
this.message("出现错误,请检查图片格式!");
return null;
}
}
private void message(string msg)
{
HttpContext.Current.Response.Write("<script language=javascript>alert('" + msg + "');</script>");
}
private void message(string msg, string url)
{
HttpContext.Current.Response.Write("<script language=javascript>alert('" + msg + "');window.location='" + url + "'</script>");
}
public string FileType
{
set
{
this.fileType = value;
}
}
public string FName
{
get
{
return this.fName;
}
set
{
this.fName = value;
}
}
public string Path
{
set
{
this.path = @"\" + value + @"\";
}
}
public int Sizes
{
set
{
this.sizes = value * 0x400;
}
}
}
申明
非源创博文中的内容均收集自网上,若有侵权之处,请及时联络,我会在第一时间内删除.再次说声抱歉!!!
博文欢迎转载,但请给出原文连接。