Asp.Net的FileUpload类实现上传文件实例
本文实例讲述了asp.net的fileupload类实现上传文件的方法。分享给大家供大家参考。
具体功能代码如下:
using system.collections.generic;
using system.text;
using system.web.ui;
using system.web;
using system.web.ui.webcontrols;
using system.collections;
using system.drawing;
using system.drawing.imaging;
using system.io;
namespace csframework.bll
{
/// <summary>
/// 支持上传的文件类型
/// </summary>
public enum uploadfiletype
{
articleattachment = 1,
image = 2,
video = 3,
all = 4
}
/// <summary>
/// 上传文件管理类
/// </summary>
public class cfileupload
{
private fileupload _fileupload;
private string _savepath;
private string _lastuploadedfile = string.empty;
private bool _autogenfilename = false;
private bool _autogenwatermark = false;
public string lastuploadedfile { get { return _lastuploadedfile; } }
private string _error = "";
private string picture_file = "[.gif.png.jpeg.jpg]";
private string zip_file = "[.zip.rar]";
private string muilt_media_file = "[.mpeg.mpg.fla.wma]";
private int img_max_width = 700;//指定宽度
private int img_max_height = 0;//未指定高度
private int max_size_upload = 1024;//最大支持上传小于1mb的文件。
/// <summary>
/// 构造器
/// </summary>
/// <param name="fileupload">asp.net fileupload对象</param>
/// <param name="savepath">保存目录,不包含文件名</param>
/// <param name="autogenfilename">自动生成文件名</param>
public cfileupload(fileupload fileupload, string savepath, bool autogenfilename, bool autogenwatermark)
{
_savepath = savepath;
_fileupload = fileupload;
_autogenfilename = autogenfilename;
_autogenwatermark = autogenwatermark;
}
/// <summary>
/// 构造器
/// </summary>
/// <param name="fileupload">asp.net fileupload对象</param>
/// <param name="savepath">保存目录,不包含文件名</param>
public cfileupload(fileupload fileupload, string savepath)
{
_savepath = savepath;
_fileupload = fileupload;
}
/// <summary>
/// 上传rar文件
/// </summary>
public bool uploadrarfile()
{
return doupload(zip_file);
}
/// <summary>
/// 上传视频文件
/// </summary>
public bool uploadvideo()
{
return doupload(muilt_media_file);
}
/// <summary>
/// 上传图片文件
/// </summary>
public bool uploadimage()
{
return doupload(picture_file);
}
public bool uploadimage(int maxwidth, int maxheight)
{
this.img_max_width = maxwidth;
this.img_max_height = maxheight;
return doupload(picture_file);
}
/// <summary>
/// 上传任何支持的文件
/// </summary>
public bool uploadanysupported()
{
return doupload(picture_file zip_file muilt_media_file);
}
/// <summary>
/// 生成新的文件名
/// </summary>
private string getnewfilename(string folder, string filename)
{
if (_autogenfilename) //自动生成32位guid文件名
{
string ext = system.io.path.getextension(filename);
string newfile = guid.newguid().tostring().replace("-", "") ext;
return folder newfile;
}
else
{
if (system.io.file.exists(folder filename))
{
string ext = system.io.path.getextension(filename);
string filebody = filename.replace(ext, "");
int x = 1;
while (true) //如果文件存在,生成尾部带(x)的文件
{
string newfile = folder filebody "(" x.tostring() ")" ext;
if (!system.io.file.exists(newfile))
return folder filebody "(" x.tostring() ")" ext;
else
x ;
}
}
else
return folder filename;
}
}
/// <summary>
/// 最大支持小于1mb的文件。
/// </summary>
private bool allowmaxsize(int filelength)
{
double kb = filelength / 1024;
return (int)kb < max_size_upload;
}
private bool doupload(string allowedextensions)
{
try
{
bool fileok = false;
if (!_fileupload.hasfile) throw new exception("没有文件!"); //上传控件中如果不包含文件,退出
// 得到文件的后缀
string fileextension = system.io.path.getextension(_fileupload.filename).tolower();
// 看包含的文件是否是被允许的文件后缀
fileok = allowedextensions.indexof(fileextension) > 0;
if (!fileok) throw new exception("不支持的文件格式!");
//检查上传文件大小
fileok = allowmaxsize(_fileupload.filebytes.length);
if (!fileok) throw new exception("图片文件不能大于" max_size_upload.tostring() "kb!");
try
{
// 文件另存在服务器指定目录下
string savefile = getnewfilename(_savepath, _fileupload.filename);
if (isuploadimage(fileextension))//保存图片
{
system.drawing.image output = cimagelibrary.frombytes(_fileupload.filebytes);
// 检查图片宽度/高度/大小
if (this.img_max_width != 0 && output.width > this.img_max_width)
{
output = cimagelibrary.getoutputsizeimage(output, this.img_max_width);
}
bitmap bmp = new bitmap(output);
this.createdir(path.getdirectoryname(savefile));
bmp.save(savefile, output.rawformat);
bmp.dispose();
output.dispose();
if (_autogenwatermark)
{
watermarkimage genwatermark = new watermarkimage();
genwatermark.drawwords(savefile, appconfig.current.watermarkmain,
appconfig.current.watermarkdesc, float.parse("0.2"));
}
}
else//其它任何文件
{
this.createdir(path.getdirectoryname(savefile));
_fileupload.postedfile.saveas(savefile);
}
_lastuploadedfile = savefile;
return true;
}
catch (exception ex)
{
throw new exception("上传文件时发生未知错误!" ex.message);
}
}
catch (exception ex)
{
_error = ex.message;
return false;
}
}
private void createdir(string dir)
{
if (directory.exists(dir) == false)
directory.createdirectory(dir);
}
private bool isuploadimage(string fileextension)
{
bool isimage = picture_file.indexof(fileextension) > 0;
return isimage;
}
}
}
希望本文所述对大家的asp.net程序设计有所帮助。