using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
///
/// 自定义上传文件类
/// (可序列化)
///
[Serializable]
public class CustomFile
{
///
/// 文件全名(带路径)
///
public string FullFileName = string.Empty;
///
/// 文件名
///
public string FileName = string.Empty;
///
/// 文件大小
///
public int FileSize = 0;
///
/// 文件类型
///
public string FileContentType = string.Empty;
///
/// Byte数组
///
public byte[] FileBytes = new byte[0];
///
/// 保存文件到目录
///
/// 文件全名(带路径)
public void SaveAs(string p_FullFileName)
{
if (FileBytes.Length > 0)
{
System.IO.File.WriteAllBytes(p_FullFileName, FileBytes);
}
}
}
public partial class Default2 : System.Web.UI.Page
{
///
/// 预先定义一个Cache名称
///
private string _vName = "MyFile";
private ArrayList _FileInfo = new ArrayList();
private CustomFile Files
{
set
{
if (Cache["" + _vName + ""] != null)
{
_FileInfo = (ArrayList)Cache["" + _vName + ""];
}
else
_FileInfo = new ArrayList();
_FileInfo.Add(value);
Cache["" + _vName + ""] = _FileInfo;
}
}
///
/// 获取自定义上传文件类
///
public CustomFile[] GetFiles
{
get
{
if (Cache["" + _vName + ""] != null)
{
_FileInfo = (ArrayList)Cache["" + _vName + ""];
}
else
_FileInfo = new ArrayList();
return (CustomFile[])_FileInfo.ToArray(typeof(CustomFile));
}
}
///
/// 清除已经上载过的文件
///
public void Clear()
{
Cache.Remove("" + _vName + "");
LbFiles.Items.Clear();
}
///
/// 删除附件
///
/// 文件全名
private void DeleteFile(string p_FileName)
{
if (Cache["" + _vName + ""] != null)
{
for (int i = 0; i < GetFiles.Length; i++)
{
if (GetFiles[i].FullFileName == LbFiles.SelectedItem.Text)
{
((ArrayList)Cache["" + _vName + ""]).RemoveAt(i);
}
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Clear();
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile.FileName != "")
{
CustomFile _cf = new CustomFile();
_cf.FileBytes = FileUpload1.FileBytes;
_cf.FullFileName = FileUpload1.PostedFile.FileName;
_cf.FileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
_cf.FileSize = FileUpload1.PostedFile.ContentLength;
_cf.FileContentType = FileUpload1.PostedFile.ContentType;
Files = _cf;
LbFiles.Items.Add(_cf.FileName);
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
int ItemCount = LbFiles.Items.Count;
for (int j = 0; j < ItemCount; j++)
{
for (int i = 0; i < LbFiles.Items.Count; i++)
{
if (LbFiles.Items[i].Selected)
{
DeleteFile(LbFiles.Items[i].Text);
LbFiles.Items.Remove(LbFiles.Items[i]);
}
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
stringbind("111//222");
}
public bool stringbind(string filestringPath) //文件上传方法
{
for (int i = 0; i < LbFiles.Items.Count; i++)
{
string fileName = LbFiles.Items[i].Text.ToString();
//string fileName = LbFiles.Items[i].Value.Substring(LbFiles.Items[i].Value.LastIndexOf("//"));
//string filesName = System.IO.Path.GetFileName(LbFiles.Items[i].Value.ToString());
System.IO.Directory.CreateDirectory(Page.Server.MapPath(".") + "//" + filestringPath);
for (int s = 0; s < GetFiles.Length; s++)
{
GetFiles[i].SaveAs(Page.Server.MapPath(".") + "//" + filestringPath + "//" + fileName);
}
}
Clear();
return true;
}