gpt4 book ai didi

javascript - asp.net动态多文件上传

转载 作者:行者123 更新时间:2023-11-29 21:13:55 25 4
gpt4 key购买 nike

 protected void btnUpload_Click(object sender, EventArgs e)
{
for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFile PostedFile = Request.Files[i];
if (PostedFile.ContentLength > 0)
{
string FileName = System.IO.Path.GetFileName(PostedFile.FileName);
PostedFile.SaveAs(Server.MapPath("Files\\") + FileName);
}
}
}

<script type="text/javascript">
var counter = 0;
function AddFileUpload() {
var div = document.createElement('DIV');
div.innerHTML = '<input id="file' + counter + '" name = "file' + counter +
'" type="file" />' +
'<input id="Button' + counter + '" type="button" ' +
'value="Remove" onclick = "RemoveFileUpload(this)" />';
document.getElementById("FileUploadContainer").appendChild(div);
counter++;
}
function RemoveFileUpload(div) {
document.getElementById("FileUploadContainer").removeChild(div.parentNode);
}
</script>

上传功能可以根据用户需要动态添加。

 <div>
<span style="font-family: Arial">Click to add files</span>
<input id="Button1" type="button" value="add" onclick="AddFileUpload()" />
<div id="FileUploadContainer">
</div>
<asp:Button ID="btnUpload" runat="server"
Text="Upload" OnClick="btnUpload_Click" />

</div>

我现在面临的问题是点击上传的时候 for (int i = 0; i < Request.Files.Count; i++)这条线,Request.Files.Count always = 0 ,即使我有 1 个或多个文件要上传。我试图调试这个 Request.Files.Count 0总是..

最佳答案

只需将 FileUploadContainer div 和 Upload Button 包装在 form 中,并将其方法设置为 POST。现在您应该在 Request.Files.Count 下获取文件。下面只是一个例子

<div>
<span style="font-family: Arial">Click to add files</span>
<input id="Button1" type="button" value="add" onclick="AddFileUpload()" />
<form id="frmUploads" runat="server" method="POST">
<div id="FileUploadContainer">
</div>
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
</form>
</div>

关于javascript - asp.net动态多文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40372598/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com