using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
/// <summary>
/// Classupload 的摘要说明
/// </summary>
public class Classupload
{
public Classupload()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private System.IO.MemoryStream m;
private System.IO.Stream fs;
private string _filename; //上传文件扩展名
private int _maxlength; //获取上传文件最大长度(字节)
private string _FilePath; //获取本地上传文件路径
private int _length = 0; //保存文件长度(字节)
private string _savepath; //获取保存路径
private string _savename; //重命名后的上传文件
private string _fullpath; //上传文件的完整目录
/// <summary>
/// 上传文件成功后文件的长度(字节)
/// </summary>
public int length
{
get
{
return _length;
}
}
/// <summary>
/// 获取保存路径
/// </summary>
public string savepath
{
get
{
return _savepath;
}
}
/// <summary>
/// 获取重命名后的上传文件
/// </summary>
public string savename
{
get
{
return _savename;
}
}
/// <summary>
/// 获取上传后文件的完整目录
/// </summary>
public string fullpath
{
get
{
return _fullpath;
}
}
/// <summary>
/// 获取本地上传文件路径
/// </summary>
public string filepath
{
get
{
return _FilePath;
}
}
/// <summary>
/// 上传文件方法
/// </summary>
/// <param name="MyFile">System.Web.UI.HtmlControls.HtmlInputFile</param>
/// <param name="savepath">设置保存上传文件的路径</param>
/// <param name="maxlength">设置上传文件的最大长度</param>
public string upload(System.Web.UI.WebControls.FileUpload MyFile, string savepath, int maxlength,System.Web.UI.WebControls.Label message)
{
this._savepath = savepath;
this._maxlength = maxlength;
//得到上载文件信息和文件流
if (MyFile.PostedFile != null)
{
System.Web.HttpFileCollection oFiles;
oFiles = System.Web.HttpContext.Current.Request.Files;
if (oFiles.Count < 1)
{
return message.Text="请选择文件!";
}
_FilePath = oFiles[0].FileName;
if (_FilePath == "" || _FilePath == null)
{
return message.Text = "请选择一个文件!";
}
_filename = "." + _FilePath.Substring(_FilePath.LastIndexOf(".") + 1);
//_contentlength = oFiles[0].ContentLength.ToString();
//Response.Write(FileName+FilePath);
if (oFiles[0].ContentLength > this._maxlength)
{
return message.Text = "上传文件长度过大";
}
else
{
try
{
//处理上载的文件流信息。
byte[] b = new byte[oFiles[0].ContentLength];
//System.IO.Stream fs;
fs = (System.IO.Stream)oFiles[0].InputStream;
fs.Read(b, 0, oFiles[0].ContentLength);
//调用处理内存流方法UploadFile
return this.UploadFile(b, this._filename, this._savepath);
//int value = Convert.ToInt32(asy.AsyncState);
}
catch (Exception ex)
{
//Response.Write(ex.Message);
return ex.Message;
}
finally
{
fs.Close();
}
}
}
else
{
//Response.Write("请选择文件");
return message.Text = "请选择文件";
}
}
private string UploadFile(byte[] fs, string FileName, string savepath)
{
try
{
//定义并实例化一个内存流,以存放提交上来的字节数组。
m = new MemoryStream(fs);
this._length = fs.Length;
//定义实际文件对象,保存上载的文件。
this._fullpath = this._savepath + names() + FileName;
FileStream f = new FileStream(this._fullpath, FileMode.Create);
this._savename = names() + FileName;
//把内内存里的数据写入物理文件
m.WriteTo(f);
m.Close();
f.Close();
f = null;
m = null;
return "文件已经上传成功!";
}
catch (Exception ex)
{
return ex.Message;
}
}
//生成 (年,月,日,时,分,秒)+随机数的文件名
private string names()
{
Random rm = new Random(System.Environment.TickCount);
return System.DateTime.Now.ToString("yyyyMMddhhmmss") + rm.Next(1000, 9999).ToString();
}
/* 创建一个web窗体,拖出一个FileUpload,Label,button控件,在button的onclick事件中写上下面代码,就可实现上传文件的功能
* 四个参数的设置:第一个:是一个FileUpload控件的对象,
* 第二个:是上传的路径,
* 第三个:是成功上传后文件的最大值,
* 第四个:是显示错误信息的Label控件
* * Classupload upload = new Classupload();
upload.upload(this.FileUpload1, Server.MapPath("upload/"), 7000,this.Label1);
* */
}