- Back to Home »
- ASP.Net , JAVASCRIPT »
- Dynamic Upload Button
Posted by : ஆனந்த் சதாசிவம்
Wednesday, May 22, 2013
Javascript:
function fnAddoption()
{
var Cnt = parseInt(document.getElementById("<%=hiddencount1.ClientID %>").value);
var Cid = parseInt(document.getElementById("<%=hidrowid.ClientID %>").value);
browser = window.navigator.appName;
var newtr;
if (browser == "Microsoft Internet Explorer")
newtr = document.getElementById('Addimg').insertRow();
else
newtr = document.getElementById('Addimg').insertRow(Cid + 1);
var newtd1 = newtr.insertCell(0);
var newtd2 = newtr.insertCell(1);
var newtd4 = newtr.insertCell(2);
var newtd3 = newtr.insertCell(3);
newtd1.innerHTML = "";
newtd2.innerHTML += "<td align='right' width='219'><input class='formTxtBox' style='width:210px; height:20px;' type='file' id='txtGalleryFile" + (Cid) + "' name ='txtGalleryFile" + (Cid) + "' class='txt_box_n' contenteditable='false' value='' onKeyDown='return false;' onKeyPress='return false;' ></td>";
newtd2.height = "";
//newtd2.innerHTML +="<td align='right'>"
newtd4.innerHTML += "<td align='right' width='219'><input type='button' name='btnDel' value=' -' style='height:20px;' onClick=fndel1(this.parentNode.parentNode.rowIndex) >";
newtd4.innerHTML += "<input type='hidden' name='hidImage" + (Cid) + "' id='hidImage" + (Cid) + "' value=''>";
newtd3.innerHTML += "";
document.getElementById("<%=hiddencount1.ClientID %>").value = parseInt(document.getElementById("<%=hiddencount1.ClientID %>").value) + 1;
document.getElementById("<%=hidrowid.ClientID %>").value = parseInt(document.getElementById("<%=hidrowid.ClientID %>").value) + 1;
return true;
}
function fndel1(no, delid)
{
if (delid != undefined) {
document.getElementById('hiddeletIds').value += delid + ",";
}
document.getElementById('Addimg').deleteRow(no);
document.getElementById("<%=hiddencount1.ClientID %>").value = parseInt(document.getElementById("<%=hiddencount1.ClientID %>").value) - 1;
document.getElementById("<%=hidrowid.ClientID %>").value = parseInt(document.getElementById("<%=hidrowid.ClientID %>").value) - 1;
}
Using Page:
<td>
<table cellpadding="0" cellspacing="0" id="Addimg" height="25px" width="200px">
<tr id="trfirst" runat="server">
<td align="center">
<asp:FileUpload ID="imageFileUP" runat="server" Width="219px" />
<td>
<input type="button" id="btnincress" onclick=" return fnAddoption();" runat="server"
style="height: 20px; padding-left: 9px; top: 5px; width: 31px;" value="+" name="btnadd" />
</td>
</td>
</tr>
</table>
<asp:Label runat="Server" ID="lblgalleyFile" Style="float: left;" Height="18px" Width="82px"></asp:Label>
</td>
C# Backend Coding
....................................
#region MultipleUploader
String MultipleUploader()
{
HttpFileCollection Files;
string[] arrCount = null;
Files = Request.Files;
arrCount = Files.AllKeys;
String OldNames = String.Empty;
String ImageFileNames = String.Empty;
string filepath = AppDomain.CurrentDomain.BaseDirectory + "Gallery/";
if (!Directory.Exists(filepath))
{
Directory.CreateDirectory(filepath);
}
try
{
if (System.IO.Path.GetFileName(Files[arrCount[0]].FileName) != "")
{
String tempfile1 = Guid.NewGuid().ToString().Substring(0, 5);
Files[arrCount[0]].SaveAs(filepath + tempfile1 + ".jpg");
ImageFileNames += tempfile1 + ".jpg" + "^";
}
else
{
ImageFileNames += hidrowEditImgFirstRow.Value + "^";
}
}
catch (Exception ex) { }
for (int i = 1; i < arrCount.Length; i++)
{
try
{
String tempname = Guid.NewGuid().ToString().Substring(0, 5);
if (System.IO.Path.GetFileName(Files[arrCount[i]].FileName.Replace("^", "")) != "")
{
Files[arrCount[i]].SaveAs(filepath + tempname + ".jpg");
ImageFileNames += tempname + ".jpg" + "^";
//DBclass.fnThumbImg(filepath, ImageFileNames, "image");
}
else
{
try
{
ImageFileNames += Request.Form["hidImage" + (i - 1)].ToString() + "^";
}
catch (Exception ex)
{
}
}
}
catch (Exception ex)
{
}
}
return ImageFileNames.Substring(0, ImageFileNames.Length - 1);
}
#endregion
File Name:
..........................
string Upload = MultipleUploader();