Multiple File Upload using Javascript in asp.net.................................

Unknown | 6:24 AM |

<script type = "text/javascript">

        var counter = 0;

        function AddFileUpload() {

            var div = document.createElement('DIV');

            div.innerHTML = '<input id="file' + counter + '" name = "file' + counter +

                     '" type="file" />' +

                     '<input id="Button' + counter + '" type="button" ' +

                     'value="Remove" onclick = "RemoveFileUpload(this)" />';

            document.getElementById("FileUploadContainer").appendChild(div);

            counter++;

        }

        function RemoveFileUpload(div) {

            document.getElementById("FileUploadContainer").removeChild(div.parentNode);

        }

</script>

<form id="form1" runat="server" enctype="multipart/form-data" method = "post">

<table >
        <tbody>
            <tr>
         
                <td>
                     <span style ="font-family:Arial">Click to add files</span>&nbsp;&nbsp;

    <input id="Button1" type="button" value="add" onclick = "AddFileUpload()" />

    <br /><br />

    <div id = "FileUploadContainer">

        <!--FileUpload Controls will be added here -->

    </div>

    <br />

    <%--<asp:Button ID="btnUpload" runat="server"

      Text="Upload" OnClick="btnUpload_Click" />--%>
                </td>
            </tr>
            <tr>
                <td  align="left">
                <hr />
                    <asp:Label runat="server" ID="lblFiles"></asp:Label>
                    </td>
            </tr>
        </tbody>
    </table>

</form>





  for (int i = 0; i < Request.Files.Count; i++)
            {

                HttpPostedFile PostedFile = Request.Files[i];

                if (PostedFile.ContentLength > 0)
                {

                    string FileName = System.IO.Path.GetFileName(PostedFile.FileName);
                    Stream fs = PostedFile.InputStream;
                    BinaryReader br = new BinaryReader(fs);
                    byte[] ba = br.ReadBytes((Int32)fs.Length);

                    fid = cls.GenerateID("out_register_files", "id");
                    //db.executeNonQuery("insert into in_register_files(id,filename,contenttype,filecontent,in_register_id) values('" + fid  + "','" + PostedFile.FileName + "','" + PostedFile.ContentType + "','" + ba + "','" + iid + "')", cls.conStr);
                    //db.close();

                    cls.con.Open();
                    SqlCommand cmd = new SqlCommand("insert into out_register_files(id,filename,contenttype,filecontent,out_register_id) values(@id,@filename,@contenttype,@filecontent,@in_register_id)", cls.con);
                    cmd.Parameters.Add("@id", SqlDbType.BigInt, 0).Value = Convert.ToInt16(fid);
                    cmd.Parameters.Add("@filename", SqlDbType.VarChar).Value = PostedFile.FileName;
                    cmd.Parameters.Add("@contenttype", SqlDbType.VarChar).Value = PostedFile.ContentType;
                    cmd.Parameters.Add("@filecontent", SqlDbType.Image).Value = ba;
                    cmd.Parameters.Add("@in_register_id", SqlDbType.BigInt, 0).Value = iid;
                    int count = cmd.ExecuteNonQuery();
                    cls.con.Close();
                }

            }

Category:

About http://dotnetvisual.blogspot.in/:
DOT NET TO ASP.NET is a web application framework marketed by Microsoft that programmers can use to build dynamic web sites, web applications and web services. It is part of Microsoft's .NET platform and is the successor to Microsoft's Active Server Pages (ASP) technology. ASP.NET is built on the Common Language Runtime, allowing programmers to write ASP.NET code using any Microsoft .NET language. create an application very easily ....