我们有时候作上传功能的时候会让用户上传多个图片或文件,一个一个上传吧有点麻烦。如果做成死的,一次上次,三个,或是五个的,有时候还不够用,这就很烦了。下面这种方法,在上传的地方加一个按钮,如果用户想上传几个就点几次,这样就会出现多个上传框,让他选择,好了,不多说了。下面是代码
前台代码
http://www.w3.org/1999/xhtml" >
无标题页
这里有一点要注意的是)" />这里面的MaxFileCounts。这个是用户上传最大数的一个限制。你可以在这里写成死值,也可以在CS进行配置,主要看你的需求。
下面是cs文件了
这里的MaxFileCount是在我配置文件写的,你可以根据你的情况写。
public int MaxFileCounts = MaxFileCount;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
HttpFileCollection fileList = HttpContext.Current.Request.Files;
if (fileList == null)
{
return;
}
FileImage file = new FileImage();
try
{
for (int i = 0; i < fileList.Count; i++)
{
HttpPostedFile postedFile = fileList[i];
if (postedFile == null)
continue;
string fileName = Path.GetFileNameWithoutExtension(postedFile.FileName);
string extension = Path.GetExtension(postedFile.FileName);
if (string.IsNullOrEmpty(extension) == true)
continue;
bool flag = false;
foreach (string ext in AllowFileList)
{
if (ext == extension.ToLower())
{
flag = true;
}
}
if (flag == false)
continue;
string storeUrl = DateTime.Now.ToString("yyyyMMddhhmmss") + DateTime.Now.Millisecond.ToString() + extension.ToString();
string Url = storeFilePath + storeUrl;
string fullPath = Server.MapPath(Url);
postedFile.SaveAs(fullPath);
Hashtable ht = new Hashtable();
ht.Add("Title",fileName);
ht.Add("imgUrl",storeUrl);
ht.Add("imgType",postedFile.ContentType);
ht.Add("imgSize",postedFile.ContentLength);
file.insertImage(ht);//这里是我的添加语句,你可写成你自己的。
}
}
catch (Exception ex)
{
this.Label1.Text = ex.Message;
}
}
本文作者:chenguang79 来源:http://blog.csdn.net/chenguang79/
CIO之家 www.ciozj.com 微信公众号:imciow