Friday, August 6, 2010

Upload multiple files

In .aspx file

[script type="text/javascript"]
function addElement() {
var ni = document.getElementById('myDiv');
var numi = document.getElementById('theValue');
var num = (document.getElementById('theValue').value - 1) + 2;
numi.value = num;
var newdiv = document.createElement('div');
var divIdName = 'my' + num + 'Div';
newdiv.setAttribute('id', divIdName);
newdiv.innerHTML = '[input type="file" name="attachment" id="attachment"/][input type="Button" value="Remove" onclick="removeElement(' + divIdName
+ ')"/]';
ni.appendChild(newdiv);
}


function removeElement(divNum) {
var d = document.getElementById('myDiv');
d.removeChild(divNum);
}

[/script]
[table]
[tr]
[td style="width: 295px"]
Upload Image Logo:
[/td]
[td]
[input type="file" name="attachment" runat="server" id="attachment" onchange="document.getElementById('moreUploadsLink').style.display =
'block';" /]
[div id="moreUploadsLink" style="display: none;"]
[a href="javascript:addElement();" style="font-size:12px;color:White;"]Upload More Images For Sliding[/a]
[/div]
[input type="hidden" value="0" id="theValue" /]
[div id="myDiv"]
[/div]
[/td]
[/tr]
[/table]
In .cs file


HttpFileCollection uploadFiles = HttpContext.Current.Request.Files;
for (int i = 1; i < uploadFiles.Count; i++)
{

HttpPostedFile uploadFile = uploadFiles[i];
if (uploadFile.FileName != null && uploadFile.FileName != "")
{
try
{
uploadFile.SaveAs(Server.MapPath("~/flash/banner/content/images/") + "\\" + Path.GetFileName(uploadFile.FileName));
strImageUrl = "~/flash/banner/content/images/" + Path.GetFileName(uploadFile.FileName);
strImageName = Path.GetFileName(uploadFile.FileName);

tblAlbum objAlbum = new tblAlbum();
objAlbum.PicName = strImageName;
objAlbum.PicUrl = strImageUrl;
objAlbum.BrandId = intBrandId;
db.tblAlbums.InsertOnSubmit(objAlbum);
db.SubmitChanges();
}
catch (Exception ex)
{
}

}
}

No comments: