SIGN UP MEMBER LOGIN:    
ARTICLE

JavaScript function: To upload multiple images

Posted by Purushottam Rathore Articles | ASP.NET Programming August 06, 2010
Through this article you will learn how to create JavaScript function to upload multiple images.
Reader Level:

Through this article you will learn how to create JavaScript function to upload multiple images.


JavaScript: This is a JavaScript function to upload multiple image at a time.

<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>

Example: Uploadmultiplefiles.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Uploadmultiplefiles.aspx.cs" Inherits="Uploadmultiplefiles" %> 

<!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>Multiple Image upload using JavaScript</title>
    
<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>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<div id="fileUploadarea">
            
<asp:FileUpload ID="FileUpload1" runat="server" BorderStyle="Solid" BorderColor="Gray" /><br />
        
</div>
        
&nbsp;
        <input style="display: block;" id="btnAddMoreImages" type="button" value="Add more images" 
onclick="AddMoreImages();" />
        
<asp:Button ID="ButtonUpload" runat="server" OnClick="ButtonUpload_Click" Text="Upload" />
    
</div>
    
</form>
</body>
</html>

Uploadmultiplefiles.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Uploadmultiplefiles : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {
    }

    
protected void ButtonUpload_Click(object sender, EventArgs e)
        {
           
HttpFileCollection httpFileCollection = Request.Files;
           
for (int i = 0; i < httpFileCollection.Count; i++)
           {
                
HttpPostedFile httpPostedFile = httpFileCollection[i];
                
if (httpPostedFile.ContentLength > 0)
               {
                    httpPostedFile.SaveAs(Server.MapPath(
"~/UploadedImages/") + System.IO.Path.GetFileName(httpPostedFile.FileName));    
        }
     }
   }
}

Output: By default you can upload single image file.

1.png

Figure 1:


If you want to upload more then one images you  will need to click on 'Add more images' as follows:

2.png
Figure 2:

Now browse the images and click on upload button. But keep in mind to create a folder as '
UploadedImages' in your application root directory.

erver'>
Login to add your contents and source code to this article
share this article :
post comment
 

i m working on a project in which i want to give uploading and downloading (MP3 Format) functionality to user using C#. i m a beginner in DOT NET technology and don't know much about it. please help me....

Posted by vishal shrivastav Apr 05, 2011

I was going to suggest almost same to be more descriptive and write more about how the code works and what classes are used. Good work Puru.

Posted by Mahesh Chand Aug 15, 2010

Hi Purushottam,

This is a good topic and good solution for our users.  The only thing I would suggest is to put some more descriptive text before every piece of code you write.  That way our readers have some understanding of what the code is doing.  It would also be cool to see the javascript rewritten in jquery (perhaps a topic for a new article?) .  Anyway, thanks for your contribution, and looking forward to seeing more contributions from you.

Posted by Mike Gold Aug 14, 2010
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor