RSS

Tag Archives: c# load multiple files dynamically

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


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: