回答
FileUpload.AllowMultiple
属性.NET 4.5及更高版本将允许您控制选择多个文件。
<asp:FileUpload ID="fileImages" AllowMultiple="true" runat="server" />
.NET 4及以下
<asp:FileUpload ID="fileImages" Multiple="Multiple" runat="server" />
在后回,则可以:
Dim flImages As HttpFileCollection = Request.Files
For Each key As String In flImages.Keys
Dim flfile As HttpPostedFile = flImages(key)
flfile.SaveAs(yourpath & flfile.FileName)
Next
还有其他的选择,你可以使用这些控件具有多个上传选项并且这些控件也支持Ajax
为什么不使用闪光灯的JavaScript &上传控制?
这里是你如何选择和使用文件上传控件上传多个文件在asp.net 完整的示例....
写这段代码在.aspx文件..
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<div>
<input type="file" id="myfile" multiple="multiple" name="myfile" runat="server" size="100" />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<br />
<asp:Label ID="Span1" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
后写在.aspx.cs文件此代码..
protected void Button1_Click(object sender,EventArgs e) {
string filepath = Server.MapPath("\\Upload");
HttpFileCollection uploadedFiles = Request.Files;
Span1.Text = string.Empty;
for(int i = 0;i < uploadedFiles.Count;i++) {
HttpPostedFile userPostedFile = uploadedFiles[i];
try {
if (userPostedFile.ContentLength > 0) {
Span1.Text += "<u>File #" + (i + 1) + "</u><br>";
Span1.Text += "File Content Type: " + userPostedFile.ContentType + "<br>";
Span1.Text += "File Size: " + userPostedFile.ContentLength + "kb<br>";
Span1.Text += "File Name: " + userPostedFile.FileName + "<br>";
userPostedFile.SaveAs(filepath + "\\" + Path.GetFileName(userPostedFile.FileName));
Span1.Text += "Location where saved: " + filepath + "\\" + Path.GetFileName(userPostedFile.FileName) + "<p>";
}
} catch(Exception Ex) {
Span1.Text += "Error: <br>" + Ex.Message;
}
}
}
}
并在这里你去...你的多个文件上传控制准备好了。祝你有美好的一天。
这段代码非常感谢。然而HttpPostedFile.ContentLength是以字节表示的,而不是以千比特来表示的...... –
你可以试试下面的代码:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim j As Integer = 0
Dim hfc As HttpFileCollection = Request.Files
Dim PathName As String
For i As Integer = 0 To hfc.Count - 1
Dim hpf As HttpPostedFile = hfc(i)
If hpf.ContentLength > 0 Then
hpf.SaveAs(Server.MapPath("~/E:\") & System.IO.Path.GetFileName(hpf.FileName))
PathName = Server.MapPath(hpf.FileName)
If j < hfc.Count Then
Dim strConnString As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString
Dim sqlquery As String
sqlquery = "Insert_proc"
Dim con As New SqlConnection(strConnString)
Dim cmd As New SqlCommand(sqlquery, con)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@FilePath", SqlDbType.VarChar).Value = PathName
j = j + 1
Try
con.Open()
cmd.ExecuteNonQuery()
Catch ex As Exception
Throw ex
Finally
con.Close()
con.Dispose()
End Try
If j = hfc.Count Then
Exit Sub
End If
End If
End If
Next
Catch generatedExceptionName As Exception
Throw
End Try
End Sub
我只碰到过这样的精辟简单的解决方案,如果您使用的是.NET 4.5(而不是在低版本,很容易支持此),你可以使用jQuery做的事情真的简单而无痛。
Uploading Multiple Files Using jQuery and Generic Handler in ASP.Net 4.5
当然有传统的ASP商业版本可在ASP Uploader
Default.aspx的代码
<asp:FileUpload runat="server" id="fileUpload1" Multiple="Multiple">
</asp:FileUpload>
<asp:Button runat="server" Text="Upload Files" id="uploadBtn"/>
Default.aspx的被发现。VB
Protected Sub uploadBtn_Click(sender As Object, e As System.EventArgs) Handles uploadBtn.Click
Dim ImageFiles As HttpFileCollection = Request.Files
For i As Integer = 0 To ImageFiles.Count - 1
Dim file As HttpPostedFile = ImageFiles(i)
file.SaveAs(Server.MapPath("Uploads/") & file.FileName)
Next
End Sub
步骤1:添加
<asp:FileUpload runat="server" id="fileUpload1" Multiple="Multiple">
</asp:FileUpload>
步骤2:添加
Protected Sub uploadBtn_Click(sender As Object, e As System.EventArgs) Handles uploadBtn.Click
Dim ImageFiles As HttpFileCollection = Request.Files
For i As Integer = 0 To ImageFiles.Count - 1
Dim file As HttpPostedFile = ImageFiles(i)
file.SaveAs(Server.MapPath("Uploads/") & ImageFiles(i).FileName)
Next
End Sub
这实际上起作用了,应该是被接受的答案。 – Rich
要添加的多个文件使用以下代码
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.fileUpload{
width:255px;
font-size:11px;
color:#000000;
border:solid;
border-width:1px;
border-color:#7f9db9;
height:17px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<div id="fileUploadarea"><asp:FileUpload ID="fuPuzzleImage" runat="server" CssClass="fileUpload" /><br /></div><br />
<div><input style="display:block;" id="btnAddMoreFiles" type="button" value="Add more images" onclick="AddMoreImages();" /><br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Upload" />
</div>
</div>
<script language="javascript" type="text/javascript">
function AddMoreImages() {
if (!document.getElementById && !document.createElement)
return false;
var fileUploadarea = document.getElementById("fileUploadarea");
if (!fileUploadarea)
return false;
var newLine = document.createElement("br");
fileUploadarea.appendChild(newLine);
var newFile = document.createElement("input");
newFile.type = "file";
newFile.setAttribute("class", "fileUpload");
if (!AddMoreImages.lastAssignedId)
AddMoreImages.lastAssignedId = 100;
newFile.setAttribute("id", "FileUpload" + AddMoreImages.lastAssignedId);
newFile.setAttribute("name", "FileUpload" + AddMoreImages.lastAssignedId);
var div = document.createElement("div");
div.appendChild(newFile);
div.setAttribute("id", "div" + AddMoreImages.lastAssignedId);
fileUploadarea.appendChild(div);
AddMoreImages.lastAssignedId++;
}
</script>
</form>
</body>
</html>
服务器端代码:
try
{
HttpFileCollection hfc = Request.Files;
for (int i = 0; i < hfc.Count; i++)
{
HttpPostedFile hpf = hfc[i];
if (hpf.ContentLength > 0)
{
hpf.SaveAs(Server.MapPath("~/uploads/") +System.IO.Path.GetFileName(hpf.FileName);
}
}
}
catch (Exception)
{
throw;
}
aspx code
<asp:FileUpload ID="FileUpload1" runat="server" AllowMultiple="true" />
<asp:Button ID="btnUpload" Text="Upload" runat="server" OnClick ="UploadMultipleFiles" accept ="image/gif, image/jpeg" />
<hr />
<asp:Label ID="lblSuccess" runat="server" ForeColor ="Green" />
Code Behind:
protected void UploadMultipleFiles(object sender, EventArgs e)
{
foreach (HttpPostedFile postedFile in FileUpload1.PostedFiles)
{
string fileName = Path.GetFileName(postedFile.FileName);
postedFile.SaveAs(Server.MapPath("~/Uploads/") + fileName);
}
lblSuccess.Text = string.Format("{0} files have been uploaded successfully.", FileUpload1.PostedFiles.Count);
}
在.NET 4.5 FileUpload.AllowMultiple性能和更高的将允许您控制选择多个文件。
低于4.5像4.0(VS 2010),我们可以使用jQuery在单一控制多文件上传,使用,2 js文件: http://code.jquery.com/jquery-1.8.2.js和http://code.google.com/p/jquery-multifile-plugin/
在aspx文件上传标签,添加像类=“多“
<asp:FileUpload ID="FileUpload1" class="multi" runat="server" />
如果您想要工作示例,请转至link下载示例。
- 1. 如何选择多个文件上传?
- 2. 如何使用Kendo UI文件上传控件上传文件?
- 3. 多个文件选择和上传
- 4. 在apache tomcat上使用文件上传控件上传多个文件
- 5. 文件上传:如何检查是否使用多部分文件上传选择任何文件
- 6. 如何让用户选择多个文件上传?
- 7. 如何我使用FileUpload控件上传的文件都被选择的文件路径FileUpload控件asp.net
- 8. 如何确定用户是否选择文件上传文件?
- 9. 使用文件上传控件上传和删除文件asp.net
- 10. 插件plupload没有上传单个选择多个文件
- 11. 使用jquery文件上传器的多个文件上传
- 12. 使用角度文件上传来上传多个文件
- 13. 使用jquery文件上传器和PHP上传多个文件
- 14. 如何用OpenFileDialog选择多个文件?
- 15. EXTJS 4.1 - 多选择并上传文件
- 16. 如何用java插件选择多个文件,然后用PHP上传?
- 17. 如何选择和上传python文件
- 18. 几个文件上传控件只上传一个文件
- 19. 如何使用选择的文本文件并上传到solaris?
- 20. 文件上传控件的错误使用文件上传控件,在这里
- 21. 如何用expressjs上传多个文件?
- 22. 上传文件在jquery文件上传插件中选择的文件
- 23. 如何使ASP文件上传控件接受大文件?
- 24. jquery文件上传(blueimp) - IE9不支持多文件选择?
- 25. Jquery多文件上传无需选择文件
- 26. 如何上传文件而不选择文件?
- 27. 在Ruby on Rails中上传文件:如何选择文件夹
- 28. 如何在ICEfaces中选择上传文件的文件名?
- 29. ASP.NET文件上传:如何选择文件后自动回发?
- 30. Ajax多个文件上传,多个文件没有上传
我会从谷歌搜索开始'asp.net文件上传多个'或看看[这里](http://stackoverflow.com/questions/5508505/multi-file-upload-using-c-sharp-on-asp - 网络4-0环境),[这里](http://stackoverflow.com/questions/1222330/multiple-file-selection-for-uploading-in-asp-net)或[这里](http://stackoverflow.com/questions/3550154/select-multiple-files-to-upload-in-asp-net-preferably-without-flash)在堆栈溢出 – Basic
@Basic - 讽刺。 1年后,我已经使用'asp.net文件上传倍数'来搜索,并且您的评论意味着这将出现在最前面。 –
@Rudi对不起!至少我给了三个很好的链接... – Basic