这里附加一下上传单个文件的CS代码:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> protected void upload_Click( object sender, EventArgs e)
{
if (upfile.HasFile)
{
string fname = upfile.PostedFile.FileName;
int fi = fname.LastIndexOf( " \\ " ) + 1 ;
string filename = fname.Substring(fi);
upfile.PostedFile.SaveAs(Server.MapPath( " upload\\ " + filename));
Response.Write( " " );
}
else
{
Response.Write( " " );
}
}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> protected void upload_Click( object sender, EventArgs e)
{
if (upfile.HasFile)
{
string fname = upfile.PostedFile.FileName;
int fi = fname.LastIndexOf( " \\ " ) + 1 ;
string filename = fname.Substring(fi);
upfile.PostedFile.SaveAs(Server.MapPath( " upload\\ " + filename));
Response.Write( " " );
}
else
{
Response.Write( " " );
}
}
下面是上传多个文件的全部代码,第一次实现这样的功能,难免有考虑不周全的地方,还望高手见到后指教!
【显示页面代码】:
在
标签中插入如下脚本代码:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> < script type = " text/javascript " >
function addfile()
{
var uploadfiles = document.getElementById( " uploadfiles " ),
str = ' ' ;
uploadfiles.innerHTML += str;
}
< / script>
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> < script type = " text/javascript " >
function addfile()
{
var uploadfiles = document.getElementById( " uploadfiles " ),
str = ' ' ;
uploadfiles.innerHTML += str;
}
< / script>
在页面主体部分插入:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> < div >
< p id ="uploadfiles" >< input type ="file" size ="50" name ="file" > p >
< input onclick ="addfile()" type ="button" value ="增加" >
< asp:button id ="uploadbutton" Text ="开始上传" Runat ="server" > asp:button >
div >
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> < div >
< p id ="uploadfiles" >< input type ="file" size ="50" name ="file" > p >
< input onclick ="addfile()" type ="button" value ="增加" >
< asp:button id ="uploadbutton" Text ="开始上传" Runat ="server" > asp:button >
div >
【C#代码】:
添加using指令:using System.Text.RegularExpressions;
在protected void Page_Load(object sender, EventArgs e){}中插入如下代码:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> HttpFileCollection files = HttpContext.Current.Request.Files;
// 状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
for ( int ifile = 0 ; ifile < files.Count; ifile ++ )
{
HttpPostedFile postedfile = files[ifile];
string filename, fileExt;
filename = System.IO.Path.GetFileName(postedfile.FileName); // 获取文件名
fileExt = System.IO.Path.GetExtension(filename); // 获取文件后缀
int MaxAllowUploadFileSize = Convert.ToInt32(System.Configuration.ConfigurationSettings.
AppSettings[ " MaxAllowUploadFileSize " ]); // 定义允许上传文件大小
if (MaxAllowUploadFileSize == 0 ) { MaxAllowUploadFileSize = 26500 ; }
string allowexts = System.Configuration.ConfigurationSettings.AppSettings[ " AllowUploadFileType " ];
// 定义允许上传文件类型
if (allowexts == "" ) { allowexts = " doc|docx|rar|xls|xlsx|txt " ; }
Regex allowext = new Regex(allowexts);
if (postedfile.ContentLength < MaxAllowUploadFileSize && allowext.IsMatch(fileExt)) // 检查文件大小及扩展名
{
postedfile.SaveAs(Server.MapPath( " upload\\ " + filename + fileExt)); // upload为与本页面同一目录,可自行修改
}
else
{
Response.Write( " " + fileExt + " 或文件过大') " );
}
}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> HttpFileCollection files = HttpContext.Current.Request.Files;
// 状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
for ( int ifile = 0 ; ifile < files.Count; ifile ++ )
{
HttpPostedFile postedfile = files[ifile];
string filename, fileExt;
filename = System.IO.Path.GetFileName(postedfile.FileName); // 获取文件名
fileExt = System.IO.Path.GetExtension(filename); // 获取文件后缀
int MaxAllowUploadFileSize = Convert.ToInt32(System.Configuration.ConfigurationSettings.
AppSettings[ " MaxAllowUploadFileSize " ]); // 定义允许上传文件大小
if (MaxAllowUploadFileSize == 0 ) { MaxAllowUploadFileSize = 26500 ; }
string allowexts = System.Configuration.ConfigurationSettings.AppSettings[ " AllowUploadFileType " ];
// 定义允许上传文件类型
if (allowexts == "" ) { allowexts = " doc|docx|rar|xls|xlsx|txt " ; }
Regex allowext = new Regex(allowexts);
if (postedfile.ContentLength < MaxAllowUploadFileSize && allowext.IsMatch(fileExt)) // 检查文件大小及扩展名
{
postedfile.SaveAs(Server.MapPath( " upload\\ " + filename + fileExt)); // upload为与本页面同一目录,可自行修改
}
else
{
Response.Write( " " + fileExt + " 或文件过大') " );
}
}
【Web.config中代码】:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> < appSettings >
< add key ="MaxAllowUploadFileSize" value ="256000" />
< add key ="AllowUploadFileType" value ="doc|docx|rar|xls|xlsx|txt" />
appSettings >
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> < appSettings >
< add key ="MaxAllowUploadFileSize" value ="256000" />
< add key ="AllowUploadFileType" value ="doc|docx|rar|xls|xlsx|txt" />
appSettings >
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-616848/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/12639172/viewspace-616848/