Reputation:
I have absolutely no idea on how to upload multiple files in asp.net using c#,with single upload button.Its not known in advance ,how many files are there. Can somebody provide me the code in c#??I would be grateful.
Thanks in advance!!
Upvotes: 1
Views: 3904
Reputation: 23413
You can create one upload input and have a button to add more dynamically using Javascript. When you click the save button, the files will all be in Request.Files.
<script type="text/javascript">
var uploadCount = 2;
function AddUpload()
{
var uploads = document.getElementById("uploads");
var id = "upload" + uploadCount;
uploads.innerHTML += ("<input type='file' id='" + id + "' name='" + id + "' />");
}
</script>
<a href="javascript: void(0);" onclick="javascript: AddUpload();">Add Upload</a>
<div id="uploads">
<asp:FileUpload runat="server" ID="upload1" />
</div>
<asp:Button runat="server" ID="btnSave" Text="Save" />
Upvotes: 0
Reputation: 6501
This is an example using multiple textboxes and browse buttons to collect the paths of up to 5 files and then uploads them at once.
DotNetJunkies File Upload Tutorial
This one from MSDN uses the File Field Control to accomplish the same thing.
There is a lot of code in both of those articles that should get you well on your way.
Upvotes: 0
Reputation: 21685
Multiple uploads are not possible using a single upload control (you'll have to upload one file, then repeat the whole process again after the first file has been uploaded).
You can use an IFrame & some JS to rig up one such control which will allow you to upload multiple files at once (But then also, only one file will be posted to the server at a time, and its for the better, for the server).
Or you can use some third party controls created using Java technology (Applets) or in Flash.
Upvotes: 1