asp.net(C#)多文件上传(源代码)vs2008
2011-01-27 13:02
429 查看
1.新建“项目”MutiFileUpload。如图所示:
2.Default.aspx如下图所示的设计。
3。Default.aspx.cs文件代码如下所示:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace MutiFileUpload
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
int FileNum = 0;
string filepath = Server.MapPath("./") + "UploadFile";
string filename = DateTime.Now.ToString("yyyyMMddHHmmss");
HttpFileCollection hfc = Request.Files;
for (int i = 0; i < hfc.Count; i++)
{
HttpPostedFile hpf = hfc[i];
if (hfc[i].ContentLength > 0)
{
hpf.SaveAs(filepath + "/" + filename + System.IO.Path.GetFileName(hpf.FileName));
FileNum++;
//uploadFiles.SaveAs(filepath + "/" + filename + System.IO.Path.GetFileName(hpf.FileName));
}
else
{
Response.Write("<script>alert('请选择你要上传的文件!')</script>");
//跳出for循环
i = hfc.Count;
break;
}
}
Response.Write("<script>alert('上传成功!')</script>");
Response.Write("共上传成功的文件个数为:" + FileNum + "个");
FileNum = null;
}
}
}
4。如果你想添加多个文件,直接添加fileupload控件就是了,后台的代码不用修改,不会影响的,后台代码会自动识别有几个要上传的文件。
2.Default.aspx如下图所示的设计。
3。Default.aspx.cs文件代码如下所示:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace MutiFileUpload
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
int FileNum = 0;
string filepath = Server.MapPath("./") + "UploadFile";
string filename = DateTime.Now.ToString("yyyyMMddHHmmss");
HttpFileCollection hfc = Request.Files;
for (int i = 0; i < hfc.Count; i++)
{
HttpPostedFile hpf = hfc[i];
if (hfc[i].ContentLength > 0)
{
hpf.SaveAs(filepath + "/" + filename + System.IO.Path.GetFileName(hpf.FileName));
FileNum++;
//uploadFiles.SaveAs(filepath + "/" + filename + System.IO.Path.GetFileName(hpf.FileName));
}
else
{
Response.Write("<script>alert('请选择你要上传的文件!')</script>");
//跳出for循环
i = hfc.Count;
break;
}
}
Response.Write("<script>alert('上传成功!')</script>");
Response.Write("共上传成功的文件个数为:" + FileNum + "个");
FileNum = null;
}
}
}
4。如果你想添加多个文件,直接添加fileupload控件就是了,后台的代码不用修改,不会影响的,后台代码会自动识别有几个要上传的文件。
相关文章推荐
- asp.net中实现多文件上传C#源代码
- asp.net(C#)上传单个图片并判断图片的类型和大小(源代码)vs2008
- ASP.NET (C#版)如何将文件上传至数据库(源代码)
- c# asp.net uploadify 上传大文件 出现的 HTTP 404 问题
- (原)ASP.NET 2.0(C#)实现多文件上传
- asp.net C# 上传较大文件失败
- asp.net(c#)如何读取上传过程中的.txt文件中的数据,并将其写入数据库的
- FCK在ASP.NET(C#)中的应用,FCK上传文件重命名!
- asp.net 上传单个文件(c#)
- [翻译]ASP.NET(C#) 中的文件上传进度条
- ASP.NET(C#)实现一次性上传多张图片(多个文件)
- asp.net,c#,同时上传多个文件
- asp.net(C#) 上传Execl文件 并插入数据到数据库实例
- asp.net/c# 用<input type="file" />实现文件上传,multipart/form-data
- ASP.NET(C#)实现一次性动态上传多张图片的代码(多个文件)
- [翻译]ASP.NET(C#) 中的文件上传进度条
- asp.net,C#,html控件的File控件实现多文件上传简单实例,vs2010
- 图片文件上传, 生成高质量缩略图 (asp.net c#)
- ASP.NET 2.0(C#)实现多文件上传
- ASP.NET(C#)实现一次性上传多张图片(多个文件)