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>
</label>
<input type=”button” onclick=”AddNewRow(); return false;” value=”More” />
<asp:Button ID=”btnAddPhoto” Text=”add photo” runat=”server”
onclick=”btnAddPhoto_Click1″ />
<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();
}
SRINIVAS
January 19, 2012 at 10:40 am
it’s helped me lot for resolving my issue
SRINIVAS
January 19, 2012 at 10:44 am
it’s helped me lot on resolve my issue
danyal316
March 27, 2012 at 5:39 pm
superb
humrahimcs
March 28, 2012 at 12:29 pm
thank for compliment dear……..
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
Vishwa
July 2, 2012 at 10:27 am
Usefull link good, it works
Gururaj Shetty
September 18, 2012 at 11:58 am
Super…Thanks a lot…..working fine
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.
ZweThiha
September 5, 2014 at 7:49 am
Thanks for useful example. Its work for me.
xdfd@gmail.com
February 25, 2015 at 8:27 am
sfdsfsdfsdfsdf