Asp.net上传多个文件【随意添加和删除个数】
程序员文章站
2022-03-03 20:02:43
项目中问题手记,网上搜集并整理正确首先上前端页面运行成功是这的前端代码<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MulitFilesUpload.aspx.cs" Inherits="WebApplication1.MulitFilesUpload" %>
项目中问题手记,网上搜集并整理正确
首先上前端页面
运行成功是这的
前端代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MulitFilesUpload.aspx.cs" Inherits="WebApplication1.MulitFilesUpload" %>
<!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 id="Head1" runat="server">
<title>多文件上传</title>
<script type="text/javascript" src="../Lib/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
function AddAttachments() {
document.getElementById('attach').innerText = "继续添加附件";
tb = document.getElementById('attAchments');
newRow = tb.insertRow();
newRow.insertCell().innerHTML = "<input name='File' size='50' type='file'> <input type=button value='删除' onclick='delFile(this.parentElement.parentElement.rowIndex)'>";
}
function delFile(index) {
document.getElementById('attAchments').deleteRow(index);
tb.rows.length > 0 ? document.getElementById('attach').innerText = "继续添加附件" : document.getElementById('attach').innerText = "添加附件";
}
</script>
</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<div>
<div><table id="attAchments"></table></div><span>
<img src="icoAddFl.gif"/> </span> <a id="attach" style="font-family:宋体;font-size:9pt;"
title="如果您要发送多个附件,您只需多次点击“继续添加附件”即可, 要注意附件总量不能超过发送限制的大小。" onclick="AddAttachments();"
href="javascript:;" name="attach">添加附件</a>
</div>
<asp:Button ID="btnSave" runat="server" Text="保存" OnClick="btnSave_Click" />
</form>
</body>
</html>
都是来自网上的摘抄,运行无误
后台程序
protected void btnSave_Click(object sender, EventArgs e)
{
int fileCount = Request.Files.Count;
string fileName = string.Empty;
string filePath = string.Empty;
for (int i = 0; i < fileCount; i++)
{
fileName = Request.Files[i].FileName;
filePath = _saveToPath + fileName.Substring(fileName.LastIndexOf("\\") + 1);
Request.Files[i].SaveAs(filePath);
}
}
//_saveToPath 根据自己实际情况填写路径
至此,Asp.net多文件上传就轻松搞定,源码随后附上,毕竟是大家的O(∩_∩)O
本文地址:https://blog.csdn.net/jjl_sky/article/details/107359118
上一篇: Oracle数据库优化的总结
下一篇: 数据库:“主键”