首先是ASPX页面中添加file标签

<input onclick="addFile()" type="button" value="增加" /><br />
<input id="viewfile" type="file" name="File" runat="server" style="width: 300px" accept="application/pdf,application/msword,application/x-zip-compressed"  />
 描述:<input name="text" type="text" style="width: 150px" maxlength="20" />

还有按钮

<asp:Button CssClass="inputbutton" ID="BtnAdd" runat="server" Text="提交" OnClick="btnSave_Click" />

添加addFile()js事件

    <script type="text/javascript">
var i = 1
function addFile() {
if (i < 8) {
var str = '<BR> <input type="file" name="File" runat="server" style="width: 300px" accept="application/pdf,application/msword,application/x-zip-compressed"/>描述:<input name="text" type="text" style="width: 150px" maxlength="20" />'
document.getElementById('MyFile').insertAdjacentHTML("beforeEnd", str)
}
else {
alert("您一次最多只能上传8个附件!")
}
i++
}
</script>

后台处理事件

       /// <summary>
/// 保存事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnSave_Click(object sender, EventArgs e)
{
try
{
lblMessage.Text = "";
lblMessage.Visible = false;
System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
System.Text.StringBuilder strmsg = new System.Text.StringBuilder("");
string[] rd = Request.Form[].Split(',');//获得图片描述的文本框字符串数组,为对应的图片的描述
int ifile;
for (ifile = ; ifile < files.Count; ifile++)
{
if (files[ifile].FileName.Length > )
{
System.Web.HttpPostedFile postedfile = files[ifile];
if (postedfile.ContentLength / > )//单个文件不能大于10240k
{
strmsg.Append(Path.GetFileName(postedfile.FileName) + "---不能大于10240k<br>");
break;
}
string fex = Path.GetExtension(postedfile.FileName).ToLower();
if (fex != ".doc" && fex != ".docx" && fex != ".zip" && fex != ".rar")
{
strmsg.Append(Path.GetFileName(postedfile.FileName) + "---格式不对,只能是doc、docx、zip、rar");
break;
}
}
}
if (strmsg.Length <= )//说明图片大小和格式都没问题
{
//以下为创建图库目录
string dirname = "excellent";
string dirpath = Server.MapPath("/appendix");
dirpath = dirpath + "\\" + dirname;
if (Directory.Exists(dirpath) == false)
{
Directory.CreateDirectory(dirpath);
}
Random ro = new Random();
int name = ;
int id = ;
Model.ExcellentWorksModel excellentWorksModel = null;
for (int i = ; i < files.Count; i++)
{
System.Web.HttpPostedFile myFile = files[i];
string FileName = "";
string FileExtention = "";
string PicPath = "";
FileName = System.IO.Path.GetFileName(myFile.FileName);
string stro = ro.Next(, ).ToString() + name.ToString();//产生一个随机数用于新命名的图片
string NewName = ConvertDateTimeInt(DateTime.Now) + stro;
if (FileName.Length > )//有文件才执行上传操作再保存到数据库
{
FileExtention = System.IO.Path.GetExtension(myFile.FileName); string ppath = dirpath + "\\" + NewName + FileExtention;
myFile.SaveAs(ppath);
string FJname = FileName;
PicPath = ppath;
} //保存图片详细
excellentWorksModel = new ExcellentWorksModel();
excellentWorksModel.AddTime = DateTime.Now;
excellentWorksModel.Path = PicPath;
excellentWorksModel.Title = FileName;
excellentWorksModel.Description = rd[i];
excellentWorksModel.Status = ;
bool res = BusinessLogic.ExcellentWorksBll.Add(excellentWorksModel); name = name + ;//用来重命名规则的变量
}
}
else
{
lblMessage.Text = strmsg.ToString();
lblMessage.Visible = true;
}
ResponseRedirect("/Reporter/UpLoadExcellentWorks.aspx");
}
catch (Exception ex)
{
WriteErrMsg("出现异常:"+ex.Message);
}
} /// <summary>
/// DateTime时间格式转换为Unix时间戳格式
/// </summary>
/// <param name=”time”></param>
/// <returns></returns>
public int ConvertDateTimeInt(System.DateTime time)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(, , ));
return (int)(time - startTime).TotalSeconds;
}

附件信息保存表

/*==============================================================*/
/* Table: ExcellentWorks */
/*==============================================================*/
create table ExcellentWorks (
ID int not null,
Title nvarchar(200) null,
Description nvarchar(200) null,
Path nvarchar(200) null,
AddTime datetime null,
Status int null,
constraint PK_EXCELLENTWORKS primary key (ID)
)
go declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description',
'小记者优秀作品',
'user', @CurrentUser, 'table', 'ExcellentWorks'
go declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description',
'表ID',
'user', @CurrentUser, 'table', 'ExcellentWorks', 'column', 'ID'
go declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description',
'附件文件名',
'user', @CurrentUser, 'table', 'ExcellentWorks', 'column', 'Title'
go declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description',
'描述',
'user', @CurrentUser, 'table', 'ExcellentWorks', 'column', 'Description'
go declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description',
'路径',
'user', @CurrentUser, 'table', 'ExcellentWorks', 'column', 'Path'
go declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description',
'添加时间',
'user', @CurrentUser, 'table', 'ExcellentWorks', 'column', 'AddTime'
go declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description',
'状态',
'user', @CurrentUser, 'table', 'ExcellentWorks', 'column', 'Status'
go

简单原始的ASP.NET WEBFORM中多文件上传【参考其他资料修改】的更多相关文章

  1. ASP.NET Core 中的文件上传

    ASP.NET Core上传文件 ASP.NET Core使用IFormFile来读取上传的文件内容,然后将数据写入到磁盘或其它存储空间. 添加FileUpload模型,用来接收上传的文件内容. pu ...

  2. ASP.NET中的文件上传大小限制的问题

    一.文件大小限制的问题 首先我们来说一下如何解决ASP.NET中的文件上传大小限制的问题,我们知道在默认情况下ASP.NET的文件上传大小限制为2M,一般情况下,我们可以采用更改WEB.Config文 ...

  3. Asp.Net实现无刷新文件上传并显示进度条(非服务器控件实现)(转)

    Asp.Net实现无刷新文件上传并显示进度条(非服务器控件实现) 相信通过Asp.Net的服务器控件上传文件在简单不过了,通过AjaxToolkit控件实现上传进度也不是什么难事,为什么还要自己辛辛苦 ...

  4. [代码示例]用Fine Uploader+ASP.NET MVC实现ajax文件上传

    原文 [代码示例]用Fine Uploader+ASP.NET MVC实现ajax文件上传 Fine Uploader(http://fineuploader.com/)是一个实现 ajax 上传文件 ...

  5. javaWeb中,文件上传和下载

    在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用 ...

  6. Silverlight 2中实现文件上传和电子邮件发送

    Silverlight 2中实现文件上传和电子邮件发送 [收藏此页] [打印]   作者:IT168 TerryLee  2008-05-30 内容导航: 使用Web Service上传文件   [I ...

  7. JavaWeb中的文件上传和下载功能的实现

    导入相关支持jar包:commons-fileupload.jar,commons-io.jar 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用Servlet获取上 ...

  8. ASP.NET MVC下使用文件上传

    这里我通过使用uploadify组件来实现异步无刷新多文件上传功能. 1.首先下载组件包uploadify,我这里使用的版本是3.1 2.下载后解压,将组件包拷贝到MVC项目中 3.  根目录下添加新 ...

  9. 在WebBrowser中通过模拟键盘鼠标操控网页中的文件上传控件(转)

    引言 这两天沉迷了Google SketchUp,刚刚玩够,一时兴起,研究了一下WebBrowser. 我在<WebBrowser控件使用技巧分享>一文中曾谈到过“我现在可以通过WebBr ...

随机推荐

  1. 大数据开发实战:Hive优化实战1-数据倾斜及join无关的优化

    Hive SQL的各种优化方法基本 都和数据倾斜密切相关. Hive的优化分为join相关的优化和join无关的优化,从项目的实际来说,join相关的优化占了Hive优化的大部分内容,而join相关的 ...

  2. Java-JUC(九):使用Lock替换synchronized,使用Condition的await,singal,singalall替换object的wait,notify,notifyall实现线程间的通信

    Condition: condition接口描述了可能会与锁有关的条件变量.这些用法上与使用object.wait访问隐式监视器类似,但提供了更强大的功能.需要特别指出的是,单个lock可能与多个Co ...

  3. (转)总结使用Unity 3D优化游戏运行性能的经验

    http://www.199it.com/archives/147913.html 流畅的游戏玩法来自流畅的帧率,而我们即将推出的动作平台游戏<Shadow Blade>已经将在标准iPh ...

  4. GL_ACTIVE_UNIFORMS可能不会返回没有用到的uniform

    To query for the list of active uniforms in a program, you first call glGetProgramiv with the GL_ACT ...

  5. mongodb自动关闭:页面文件太小,无法完成操作

    在一台两G内存的win server 2008电脑上运行一个程序,一段时间后mongod自动停止,发现日志文件最后有这样的错误: 2014-11-30T00:32:32.914+0800 [conn3 ...

  6. Direct2D教程III——几何(Geometry)对象

    目前博客园中成系列的Direct2D的教程有 1.万一的 Direct2D 系列,用的是Delphi 2009 2.zdd的 Direct2D 系列,用的是VS中的C++ 3.本文所在的 Direct ...

  7. windows命令行的使用,去掉"半"字

    1.新启动一个命令行,如果当前sogou是中文输入法,输入之后,下面出现一个"半"字.切换到英文输入法,这个"半"字,也不会消失,怎么办? 先切换到英文输入法, ...

  8. VMware 安装Arch Linux记录

    首先说明一下我的环境. 1.VMware Workstation 10.0.1 build-1379776 2.archlinux-2014.02.01-dual.iso 首先建立虚拟机,其他的不提了 ...

  9. C#.NET常见问题(FAQ)-override覆盖和virtual虚类如何理解

    父类使用virtual关键字,可以让子类的实例完全代替基类的类成员.(前面父类virtual后面子类override),比如下面我定义一个Employee的员工的基类,给这个基类定义了Start_Wo ...

  10. 从头认识java-15.7 Map(7)-TreeMap与LinkedHashMap

    这一章节我们来讨论一下Map两个比較经常使用的实现:TreeMap与LinkedHashMap. 1.TreeMap 特性:依照key来排序 package com.ray.ch14; import ...