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;
public partial class Index : System.Web.UI.Page
{
static public ArrayList hif = new ArrayList(); //10001 保存文件列表
public int filesUploaded = 0; // 10002上传文件的数量
protected void Page_Load(object sender, EventArgs e)
{
}
//10003-----------------添加列表
protected void AddFile_Click(object sender, EventArgs e)
{
if (Page.IsPostBack==true) //这个很重要哦.呵呵.按键在第一次PAGE加载后才起作用
{
if (FindFile.HasFile) //判断浏览文本框中有没有文件内容
{
hif.Add(FindFile);//把文件完整路径添加到数组中去
FileList.Items.Add( System.IO.Path.GetFileName(FindFile.PostedFile.FileName)); //获取一个待上传的文件名
}
else
{
TipInfo.Text = "<font color=red>错误 - 必须要有上传的文件.</font>";
}
}
else
{ }
}
//10004从listbox中删除指定的文件
protected void DelFile_Click(object sender, EventArgs e)
{
if (FileList.SelectedIndex == -1) //if the Listbox's item is not selected
{
TipInfo.Text = "<font color=red>错误 - 必须指定要删除的文件.</font>";
return;
}
else if (FileList.Items.Count != 0)
{
hif.RemoveAt(FileList.SelectedIndex); //Remove the item which is selected from the ArryList
FileList.Items.Remove(FileList.SelectedItem.Text); //Remove the item which is selected in the listbox.
TipInfo.Text = "";
}
}
/// <summary>
///10005 循环上传listbox中的文件到指定的文件夹下
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Upload_Click(object sender, EventArgs e)
{
string LocalPath = Server.MapPath("UploadFiles/"); // 上传路径
string status = ""; // 上传成功后显示的文件列表
if ((FileList.Items.Count == 0) && (filesUploaded == 0)) //judge the listbox'item is nothing
{
TipInfo.Text = "<font color=red>错误 - 必须指定要上传的图片文件.</font>";
return;
}
else
{
foreach (System.Web.UI.WebControls.FileUpload HIF in hif)
{
try
{
Boolean fileOK = false;
String fileExtension = System.IO.Path.GetExtension(HIF.FileName).ToLower();
String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions)
{
fileOK = true;
}
}
if (fileOK)
{
string fn = System.IO.Path.GetFileName(HIF.PostedFile.FileName);
HIF.PostedFile.SaveAs(LocalPath + fn);
filesUploaded++;
status += fn + "<br>";
}
else
{
TipInfo.Text = "<font color=red>对不起.你上传的图片格式不对!</font>";
FileList.Items.Clear();
}
}
catch (Exception err)
{
TipInfo.Text = "上传错误 " + LocalPath
+ "<br>" + err.ToString();
}
}
if (filesUploaded == hif.Count)
{
TipInfo.Text = "共上传了 " + filesUploaded + " 个图片文件。 <br>" + status;
}
hif.Clear();
FileList.Items.Clear();
}
}
}