ASP.Net Gmail like multiple/single file upload with defined limit size

Create upload.aspx. Here is the aspx page and scripts;

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="upload.aspx.vb" Inherits="upload" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <script language='Javascript' type="text/javascript">
      function addFile() {
            var ni = document.getElementById("fileDiv");
            var objFileCount = document.getElementById("fileCount");
            var num = (document.getElementById("fileCount").value - 1) + 2;
            objFileCount.value = num;
            var newdiv = document.createElement("div");
            var divIdName = "file" + num + "Div";
            newdiv.setAttribute("id", divIdName);
            newdiv.innerHTML = '<input type="file" name="attachment" id="attachment"/><a href="#" onclick="javascript:removeFile(' + divIdName + ');">Remove </a>';

      function removeFile(divName) {
            var d = document.getElementById("fileDiv");
    <form id="form1" runat="server">
      <input type="file" name="attachment" runat="server" id="attachment" onchange="document.getElementById('moreUploadsLink').style.display = 'block';" />
      <input type="hidden" value="0" id="fileCount" />
      <div id="fileDiv">
      <div id="moreUploadsLink" >
            <a href="javascript:addFile();">Attach another File</a>
      <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
        <br />
        <asp:Label ID="lblStatus" runat="server"></asp:Label>
        for single file upload set div id="moreUploadsLink" style="display: none"

Here is the vb code behind upload.vb

Partial Class upload
    Inherits System.Web.UI.Page

    Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
        lblStatus.Text = "Uploading..."
        Dim temp As String = ""
        Dim uploadFiles As HttpFileCollection = Request.Files
        For i As Integer = 0 To uploadFiles.Count - 1
            Dim uploadFile As HttpPostedFile = uploadFiles(i)
            Dim fileName As String = System.IO.Path.GetFileName(uploadFile.FileName)
            If fileName.Trim().Length > 0 Then
                'uploadFile.SaveAs(Server.MapPath("./Others/") + fileName)
                uploadFile.SaveAs(Server.MapPath("./") + fileName)
                temp = temp & fileName & " Successfully Uploaded <br/>"
            End If
        lblStatus.Text = temp
    End Sub
End Class

Note: for single file upload set div id="moreUploadsLink" style="display: none"
To set the maximum upload size please see :

- Vinod Kotiya


Most Viewed Post

solution for facebook waiting

Connect Sony XPLOD MEX-BT2500 Bluetooth CD Receiver with ur mobile

Crystal Report : Show data horizontally (Left to right) i.e. columns as rows