实现ASP.NET中FileUpload多文件上传
这里附加一下上传单个文件的CS代码:
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("<script>alert('报名表上传成功!');</script>");
}
else
{
Response.Write("<script>alert('请选择您要上传的报名表!');</script>");
}
}
{
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("<script>alert('报名表上传成功!');</script>");
}
else
{
Response.Write("<script>alert('请选择您要上传的报名表!');</script>");
}
}
下面是上传多个文件的全部代码,第一次实现这样的功能,难免有考虑不周全的地方,还望高手见到后指教!
【显示页面代码】:
在<head></head>标签中插入如下脚本代码:
<script type="text/javascript">
function addfile()
{
var uploadfiles=document.getElementById("uploadfiles"),
str = '<INPUT type="file" size="50" NAME="File">';
uploadfiles.innerHTML+=str;
}
</script>
function addfile()
{
var uploadfiles=document.getElementById("uploadfiles"),
str = '<INPUT type="file" size="50" NAME="File">';
uploadfiles.innerHTML+=str;
}
</script>
在页面主体部分插入:
<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>
<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){}中插入如下代码:
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("<script>alert('不允许上传类型" + fileExt + "或文件过大')</script>");
}
}
//状态信息
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("<script>alert('不允许上传类型" + fileExt + "或文件过大')</script>");
}
}
【Web.config中代码】:
<appSettings>
<add key="MaxAllowUploadFileSize" value="256000" />
<add key="AllowUploadFileType" value="doc|docx|rar|xls|xlsx|txt" />
</appSettings>
<add key="MaxAllowUploadFileSize" value="256000" />
<add key="AllowUploadFileType" value="doc|docx|rar|xls|xlsx|txt" />
</appSettings>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述