javascript实现上传多个附件,并在后台C#处理
2007-05-20 23:02
417 查看
页面文件
<%@ Page AutoEventWireup="true" CodeFile="add_news.aspx.cs" Debug="true" Inherits="admin_news_xml"
Language="C#" ValidateRequest="false" %>
<%@ Register Assembly="FreeTextBox" Namespace="FreeTextBoxControls" TagPrefix="FTB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>添加新闻</title>
<script type="text/javascript" src="../js/calendar.js"></script>
<link rel="stylesheet" rev="stylesheet" type="text/css" media="screen, projection" href="../css/manage.css" />
<script type="text/javascript">
var myCalendar=new PopupCalendar("myCalendar"); //初始化控件时,请给出实例名称:myCalendar
myCalendar.Width="150px";
myCalendar.Init();
function addFile() //继续添加附件
...{
var str = '<br/><input type="file" size="60" name="File" onchange="Check_FileType(this.value)"/>';
document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str);
}
function Check_FileType(str) //检查文件类型
...{
var pos = str.lastIndexOf(".");
var lastname = str.substring(pos,str.length);
if (lastname.toLowerCase()!=".xls"&&lastname.toLowerCase()!=".rar"&&lastname.toLowerCase()!=".zip"&&lastname.toLowerCase()!=".doc"&&lastname.toLowerCase()!=".txt"&&lastname.toLowerCase()!=".wps"&&lastname.toLowerCase()!=".pdf"&&lastname.toLowerCase()!=".docx")
...{
alert("文件必须为.rar,.zip,.txt,.doc..docx,.wps,.txt,.pdf类型");
return false;
}
else
...{
return true;
}
}
</script>
<style type="text/css">
#main_content p ...{
clear: left;
padding:2px 0px 5px 5px;
margin: 0px;
width: 650px;
height:16px;
}
#main_content label ...{
font-weight: bold;float:left;width: 70px;
}
#Button1 ...{
width: 108px;
}
</style>
</head>
<body style="font-size:12px;">
<form id="form1" method="post" runat="server" enctype="multipart/form-data">
<div id="main_content">
<p id="title" style="width:650px">添加新闻</p>
<p><label>标题</label><asp:TextBox ID="txt_title" runat="server" Style=" left: 100px;top: 20px"></asp:TextBox></p>
<p><label>发布时间</label><asp:TextBox ID="txt_time" runat="server" Style="left: 100px; top: 68px"></asp:TextBox><img
onclick="getDateString(this,myCalendar,txt_time)" src="../media/calendar.gif" />
<asp:Label ID="Label2" runat="server" ForeColor="Red" Text="点击选择日期" Width="106px"></asp:Label></p>
<p><label>发布人</label><asp:TextBox ID="txt_author" runat="server" Style=" left: 100px;top: 102px"></asp:TextBox></p>
<p><label>新闻类别</label>
<asp:DropDownList ID="list_department" runat="server" Style="position: relative" Width="155px" DataSourceID="SqlDataSource1" DataTextField="name" DataValueField="id">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ahpcConnectionString %>"
SelectCommand="SELECT [id], [name] FROM [category] WHERE [name]<>'图片新闻'"></asp:SqlDataSource>
</p>
<p ><label>内容</label><span style="font-size:12px;color:Green;">若从Word粘贴,请在复制完后点<img src="../media/ftb/office2003/wordclean.gif" align="absmiddle"/>图标清除格式!</span> </p>
<FTB:FreeTextBox ID="FreeTextBox1" runat="server" AllowHtmlMode="True" AutoConfigure="Default"
ButtonDownStyle="False" ButtonOverStyle="False" ButtonPath="../media/ftb/office2003/"
ButtonType="Image" ConvertHtmlSymbolsToHtmlCodes="True" Height="500px" ImageGalleryPath="news/images"
ToolbarLayout="FontFacesMenu,FontSizesMenu,JustifyLeft,JustifyRight,JustifyCenter,JustifyFull,InsertRule,InsertDate,InsertTime,WordCount,InsertFieldSet,WordClean,WordPaste,InsertImageFromGallery,InsertTable,Cut,Copy,Find,Paste,Delete;Undo,Redo,RemoveFormat|FontBackColorsMenu,SymbolsMenu,FontForeColorsMenu,BulletedList,NumberedList,Indent,Outdent,Bold,Italic,Underline,Strikethrough,Superscript,Subscript,CreateLink,Unlink"
Width="650px">
</FTB:FreeTextBox>
//附件上传
<label style="height: 20px; line-height: 20px;">上传附件:</label>
<input id="Button1" type="button" value=" 添加更多附件" onclick="addFile()"/>
<input id="Submit1" type="submit" value="提交" onclick="this.form1.submit()" />
<asp:Label ID="Label1" runat="server" BorderColor="White" ForeColor="Red" Height="20px"
Width="333px">上传文件大小不得超过10M</asp:Label>
<p id="MyFile"><input type="file" size="60" name="File" onchange="Check_FileType(this.value)"/></p>
<p style="text-align:center; height: 14px;"></p>
<asp:HyperLink ID="HyperLink1" runat="server" Visible="False" Font-Size="14px" Target="_blank">[HyperLink1]</asp:HyperLink><br />
</div>
</form>
</body>
</html>
后台处理部分代码
//文件上传处理
HttpFileCollection files = HttpContext.Current.Request.Files;
/**//// '状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
for (int iFile = 0; iFile < files.Count; iFile++)
...{
/**////'检查文件扩展名字
bool fileOK = false;
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != "")
...{
fileExtension = System.IO.Path.GetExtension(fileName);
String[] allowedExtensions =
...{ ".doc", ".xls", ".rar", ".zip", ".wps", ".txt", "docx", "pdf", "xls" };
for (int i = 0; i < allowedExtensions.Length; i++)
...{
if (fileExtension == allowedExtensions[i])
...{
fileOK = true;
break;
}
}
if (!fileOK) Label1.Text = "不支持上传此类型文件" + fileName;
}
if (fileOK)
...{
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("~/news/" + folder + "/attachment/") + fileName);
if (attachment_filename == "") attachment_filename = fileName;
else attachment_filename = attachment_filename + "|" + fileName;
}
}
<%@ Page AutoEventWireup="true" CodeFile="add_news.aspx.cs" Debug="true" Inherits="admin_news_xml"
Language="C#" ValidateRequest="false" %>
<%@ Register Assembly="FreeTextBox" Namespace="FreeTextBoxControls" TagPrefix="FTB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>添加新闻</title>
<script type="text/javascript" src="../js/calendar.js"></script>
<link rel="stylesheet" rev="stylesheet" type="text/css" media="screen, projection" href="../css/manage.css" />
<script type="text/javascript">
var myCalendar=new PopupCalendar("myCalendar"); //初始化控件时,请给出实例名称:myCalendar
myCalendar.Width="150px";
myCalendar.Init();
function addFile() //继续添加附件
...{
var str = '<br/><input type="file" size="60" name="File" onchange="Check_FileType(this.value)"/>';
document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str);
}
function Check_FileType(str) //检查文件类型
...{
var pos = str.lastIndexOf(".");
var lastname = str.substring(pos,str.length);
if (lastname.toLowerCase()!=".xls"&&lastname.toLowerCase()!=".rar"&&lastname.toLowerCase()!=".zip"&&lastname.toLowerCase()!=".doc"&&lastname.toLowerCase()!=".txt"&&lastname.toLowerCase()!=".wps"&&lastname.toLowerCase()!=".pdf"&&lastname.toLowerCase()!=".docx")
...{
alert("文件必须为.rar,.zip,.txt,.doc..docx,.wps,.txt,.pdf类型");
return false;
}
else
...{
return true;
}
}
</script>
<style type="text/css">
#main_content p ...{
clear: left;
padding:2px 0px 5px 5px;
margin: 0px;
width: 650px;
height:16px;
}
#main_content label ...{
font-weight: bold;float:left;width: 70px;
}
#Button1 ...{
width: 108px;
}
</style>
</head>
<body style="font-size:12px;">
<form id="form1" method="post" runat="server" enctype="multipart/form-data">
<div id="main_content">
<p id="title" style="width:650px">添加新闻</p>
<p><label>标题</label><asp:TextBox ID="txt_title" runat="server" Style=" left: 100px;top: 20px"></asp:TextBox></p>
<p><label>发布时间</label><asp:TextBox ID="txt_time" runat="server" Style="left: 100px; top: 68px"></asp:TextBox><img
onclick="getDateString(this,myCalendar,txt_time)" src="../media/calendar.gif" />
<asp:Label ID="Label2" runat="server" ForeColor="Red" Text="点击选择日期" Width="106px"></asp:Label></p>
<p><label>发布人</label><asp:TextBox ID="txt_author" runat="server" Style=" left: 100px;top: 102px"></asp:TextBox></p>
<p><label>新闻类别</label>
<asp:DropDownList ID="list_department" runat="server" Style="position: relative" Width="155px" DataSourceID="SqlDataSource1" DataTextField="name" DataValueField="id">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ahpcConnectionString %>"
SelectCommand="SELECT [id], [name] FROM [category] WHERE [name]<>'图片新闻'"></asp:SqlDataSource>
</p>
<p ><label>内容</label><span style="font-size:12px;color:Green;">若从Word粘贴,请在复制完后点<img src="../media/ftb/office2003/wordclean.gif" align="absmiddle"/>图标清除格式!</span> </p>
<FTB:FreeTextBox ID="FreeTextBox1" runat="server" AllowHtmlMode="True" AutoConfigure="Default"
ButtonDownStyle="False" ButtonOverStyle="False" ButtonPath="../media/ftb/office2003/"
ButtonType="Image" ConvertHtmlSymbolsToHtmlCodes="True" Height="500px" ImageGalleryPath="news/images"
ToolbarLayout="FontFacesMenu,FontSizesMenu,JustifyLeft,JustifyRight,JustifyCenter,JustifyFull,InsertRule,InsertDate,InsertTime,WordCount,InsertFieldSet,WordClean,WordPaste,InsertImageFromGallery,InsertTable,Cut,Copy,Find,Paste,Delete;Undo,Redo,RemoveFormat|FontBackColorsMenu,SymbolsMenu,FontForeColorsMenu,BulletedList,NumberedList,Indent,Outdent,Bold,Italic,Underline,Strikethrough,Superscript,Subscript,CreateLink,Unlink"
Width="650px">
</FTB:FreeTextBox>
//附件上传
<label style="height: 20px; line-height: 20px;">上传附件:</label>
<input id="Button1" type="button" value=" 添加更多附件" onclick="addFile()"/>
<input id="Submit1" type="submit" value="提交" onclick="this.form1.submit()" />
<asp:Label ID="Label1" runat="server" BorderColor="White" ForeColor="Red" Height="20px"
Width="333px">上传文件大小不得超过10M</asp:Label>
<p id="MyFile"><input type="file" size="60" name="File" onchange="Check_FileType(this.value)"/></p>
<p style="text-align:center; height: 14px;"></p>
<asp:HyperLink ID="HyperLink1" runat="server" Visible="False" Font-Size="14px" Target="_blank">[HyperLink1]</asp:HyperLink><br />
</div>
</form>
</body>
</html>
后台处理部分代码
//文件上传处理
HttpFileCollection files = HttpContext.Current.Request.Files;
/**//// '状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
for (int iFile = 0; iFile < files.Count; iFile++)
...{
/**////'检查文件扩展名字
bool fileOK = false;
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != "")
...{
fileExtension = System.IO.Path.GetExtension(fileName);
String[] allowedExtensions =
...{ ".doc", ".xls", ".rar", ".zip", ".wps", ".txt", "docx", "pdf", "xls" };
for (int i = 0; i < allowedExtensions.Length; i++)
...{
if (fileExtension == allowedExtensions[i])
...{
fileOK = true;
break;
}
}
if (!fileOK) Label1.Text = "不支持上传此类型文件" + fileName;
}
if (fileOK)
...{
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("~/news/" + folder + "/attachment/") + fileName);
if (attachment_filename == "") attachment_filename = fileName;
else attachment_filename = attachment_filename + "|" + fileName;
}
}
相关文章推荐
- C#的附件上传的简单实现
- C#中实现同时上传多个附件
- JS 实现上传照片到服务器 和 后台处理处理这个照片
- C# 实现发送电子邮件以及上传附件
- PHP Ajax JavaScript 实现 无刷新附件上传
- javascript实现网易邮箱多附件上传功能代码
- Atitit 文件上传 架构设计 实现机制 解决方案 实践java php c#.net js javascript c++ python
- Android实现TCP断点上传,后台C#服务实现接收
- 用PHP Session和Javascript实现文件上传进度条,处理进度条
- PHP Ajax JavaScript 实现 无刷新附件上传
- sql server 关于表中只增标识问题 C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件) ajaxfileupload插件上传图片功能,用MVC和aspx做后台各写了一个案例 将小写阿拉伯数字转换成大写的汉字, C# WinForm 中英文实现, 国际化实现的简单方法 ASP.NET Core 2 学习笔记(六)ASP.NET Core 2 学习笔记(三)
- Atitit 文件上传 架构设计 实现机制 解决方案 实践java php c#.net js javascript c++ python
- 实现TCP断点上传,后台C#服务实现接收
- 附件上传插件fileinput+SpringMVC后台代码实现
- 用Canvas+Javascript FileAPI 实现一个跨平台的图片剪切、滤镜处理、上传下载工具(转)
- 用Canvas+Javascript FileAPI 实现一个跨平台的图片剪切、滤镜处理、上传下载工具
- 怎么样通过Javascript 将前台的数据传到C#后台处理
- 利用附件上载控件+Javascript实现动态上传附件功能
- Android实现TCP断点上传 后台C#服务接收
- 利用ajax实现前台javascript同c#后台MVC下的aspx.cs文件传递数据的两种方法