RSS

Dynamically Create Multiple File Upload Control with ASP.Net C#

15 Apr

Dynamically Create Multiple File Upload Control with ASP.Net C#

MultipleFileUploadControl.aspx

1:Create Javascript Function for Generate Dynamic Control

<script type=”text/javascript” language=”javascript”>

function AddNewRow() {

var rownum = 1;

var div = document.createElement(“div”)

var divid= “dv” + rownum

div.setAttribute(“ID”,divid)

rownum++

var lbl = document.createElement(“label”)

lbl.setAttribute(“ID”, “lbl” + rownum)

lbl.setAttribute(“class”, “label1”)

lbl.innerHTML = “Images”

rownum++

var _upload = document.createElement(“input”)

_upload.setAttribute(“type”, “file”)

_upload.setAttribute(“ID”, “upload” + rownum)

_upload.setAttribute(“runat”, “server”)

_upload.setAttribute(“name”,”uploads”+rownum)

rownum++

var hyp = document.createElement(“a”)

hyp.setAttribute(“style”, “cursor:Pointer”)

hyp.setAttribute(“onclick”, “return RemoveDv(‘” + divid + “‘);”);

hyp.innerHTML = “Remove”

rownum++

var br=document.createElement(“br”)

var _pdiv = document.getElementById(“Parent”)

div.appendChild(br)

div.appendChild(lbl)

div.appendChild(_upload)

div.appendChild(hyp)

_pdiv.appendChild(div)

}

function RemoveDv(obj) {

var p = document.getElementById(“Parent”)

var chld = document.getElementById(obj)

p.removeChild(chld)

}

</script>

2: Write this code in aspx body tag

<table cellpadding=”0″ cellspacing=”0″ width=”100%” border=”0″>

<tr id=”Tr1″ runat=”Server”>

<td>

<label>

Photo:</label><asp:FileUpload ID=”uploadPhoto1″ runat=”server” CssClass=”” /><br />

<div id=”Parent”>

</div>

<label>

&nbsp;</label>

<input type=”button” onclick=”AddNewRow(); return false;”  value=”More” />&nbsp;

<asp:Button ID=”btnAddPhoto” Text=”add photo” runat=”server”

onclick=”btnAddPhoto_Click1″ />&nbsp;

<asp:Button ID=”btnCancel” Text=”cancel” runat=”server” />

</td></tr></table>

CodeBehind  Code

MultipleFileUploadControl.CS

public Byte[] GetFileContent(System.IO.Stream inputstm)

{

Stream fs=inputstm;

BinaryReader br=new BinaryReader(fs);

Int32 lnt = Convert.ToInt32(fs.Length);

byte[] bytes = br.ReadBytes(lnt);

return bytes;

}

private void SavePhotoContent()

{

HttpPostedFile postFile;

string ImageName=string.Empty;

byte[] path;

string[] keys;

try{

string contentType=string.Empty;

byte[] imgContent=null;

string[] PhotoTitle;

string PhotoTitlenme;

HttpFileCollection files = Request.Files;

keys = files.AllKeys;

for (int i = 0; i < files.Count; i++)

{

postFile = files[i];

if (postFile.ContentLength > 0)

{

// postFile.SaveAs(Server.MapPath(“Uploads”) + “\\” + System.IO.Path.GetFileName(postFile.FileName));

contentType = postFile.ContentType;

path = GetFileContent(postFile.InputStream);

ImageName = System.IO.Path.GetFileName(postFile.FileName);

PhotoTitle = ImageName.Split(‘.’);

PhotoTitlenme=PhotoTitle[0];

}

}

}

catch (Exception ex){

ex.Message.ToString();

}

}

protected void btnAddPhoto_Click1(object sender, EventArgs e)

{

SavePhotoContent();

}

Advertisements
 
11 Comments

Posted by on April 15, 2011 in ASP Dot Net C#

 

Tags:

11 responses to “Dynamically Create Multiple File Upload Control with ASP.Net C#

  1. SRINIVAS

    January 19, 2012 at 10:40 am

    it’s helped me lot for resolving my issue

     
  2. SRINIVAS

    January 19, 2012 at 10:44 am

    it’s helped me lot on resolve my issue

     
  3. danyal316

    March 27, 2012 at 5:39 pm

    superb

     
    • humrahimcs

      March 28, 2012 at 12:29 pm

      thank for compliment dear……..

       
  4. navi

    April 23, 2012 at 4:34 am

    plz help if i want to save this file in my sqlserver database in single column how to create a insert query plz help

     
    • Vishwa

      July 2, 2012 at 10:29 am

      in SavePhotoContent() method u get path which is the conversion of file to byte which you uploaded you can use that byte to store in database. try it works

       
  5. Vishwa

    July 2, 2012 at 10:27 am

    Usefull link good, it works

     
  6. Gururaj Shetty

    September 18, 2012 at 11:58 am

    Super…Thanks a lot…..working fine

     
  7. user

    January 2, 2013 at 4:35 pm

    Thank you very much for this complete example. It worked great and was easy to follow. I was able to adapt it to my website for uploading documents with very few changes.

     
  8. ZweThiha

    September 5, 2014 at 7:49 am

    Thanks for useful example. Its work for me.

     
  9. xdfd@gmail.com

    February 25, 2015 at 8:27 am

    sfdsfsdfsdfsdf

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: