首先是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. STM32启动文件深度解析

    STM32启动过程全面解析,包括启动过程的介绍.启动代码的陈列以及深入解析.相对于ARM上一代的主流ARM7/ARM9内核架构,新一代Cortex内核架构的启动方式有了比较大的变化.ARM7/ARM9 ...

  2. RxJava【变换】操作符 map flatMap concatMap buffer MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  3. JS条件判断

    JavaScript 是一种可以在浏览器中运行的脚本语言,是一种弱语言(相对于C,C#,JAVA而言),只要是计算机语言就会使用到条件判断式,而JavaScript作为一种“弱”语言,它的条件判断常常 ...

  4. 【ElasticSearch】ElasticSearch-索引优化-自定义索引

    ElasticSearch-索引优化-自定义索引 es 指定 索引 字段_百度搜索 [es]创建索引和映射 - 匡子语 - 博客园 reindex,增加字段,并新增数据 - Elastic中文社区 e ...

  5. win7基于mahout推荐之用户相似度计算

    http://www.douban.com/note/319219518/?type=like win7基于mahout推荐之用户相似度计算 2013-12-03 09:19:11    事情回到半年 ...

  6. 如何使用Android studio打开eclipse项目

    转: http://blog.csdn.net/zcw93219/article/details/50770445

  7. Code optimization and organization in Javascript / jQuery

    This article is a combined effort of Innofied Javascript developers Puja Deora and Subhajit Ghosh) W ...

  8. .Net C# 5.0 规范:迭代器

    本文内容 枚举器 enumerator 接口 - IEnumerator 可枚举 enumerable 接口 - IEnumerable 产生类型 yield type 枚举器 enumerator ...

  9. android布局 - fill_parent/match_paren/wrap_content的区别

    三个属性都用来适应视图的水平或垂直大小,一个以视图的内容或尺寸为基础的布局比精确地指定视图范围更加方便. 1)fill_parent 设置一个构件的布局为fill_parent将强制性地使构件扩展,以 ...

  10. HTML页面跳转的5种方式

    下面列了五个例子来详细说明,这几个例子的主要功能是:在5秒后,自动跳转到同目录下的hello.html(根据自己需要自行修改)文件. 1) html的实现 <head> <!-- 以 ...