Upload multiple files in asp.net
Some times we need to allow user to upload as many files as he/she wants instead of fixed number of files.
So here is the procedure to achieve this. Idea is taken from Joe's video.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>
<!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 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>
I have put css in the same file for clarity.
If you want to restrict user to certain number of files then you can change your javascript function as given below.
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);
if (AddMoreImages.lastAssignedId > 104) {
alert("You can't add more than 6 images with a single puzzle");
return false;
}
var div = document.createElement("div");
div.appendChild(newFile);
div.setAttribute("id", "div" + AddMoreImages.lastAssignedId);
fileUploadarea.appendChild(div);
AddMoreImages.lastAssignedId++;
}
Now it allows 6 files ( one fixed and 5 dynamically added) maximum.
Insert the following code in code behind file of this page.
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;
}
Make sure you have created a folder named "uploads" at the root of your site and you have rights to write files there.
Revision number 1, Friday, July 09, 2010 8:50:02 PM by habdulrauf
This is not the most up to date version of this article. The most recent version can be found here.
You must Login to comment.
|
Thu, Jun 23, 2011 2:34 PM
by evanorue
|
I've a problem with the size of the file when I upload!
|
New
Sun, Feb 17, 2008 3:31 AM
by
|
FileUpload
Asp.Net 2.0 introduced a new control called FileUpload that is used to upload files from the client browser to the server. Video Simple File Uploads in ASP.NET Multiple File Uploads in ASP.NET Multiple File Uploads in ASP.NET (Elaborated) File Uploads with
|