Friday, April 27, 2012

Multiple File Upload using Javascript in asp.net


<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();
                }

            }




No comments:

Using Authorization with Swagger in ASP.NET Core

 Create Solution like below LoginModel.cs using System.ComponentModel.DataAnnotations; namespace UsingAuthorizationWithSwagger.Models {     ...