"Rajagopal" wrote:
Question
How to Upload multiple images in asp.net? (ex.Like a gmail file field)
Hi All,
<form id="form1" runat="server">
<input type="file" id="file_1" runat="server" /><input type="button"
onclick="addcon trol()"
value=" + " />
<div id="myDiv">
</div>
<br />
<asp:Button ID="btn_submit " runat="server" Text="Submit"
OnClick="btn_Cl ick" />
<br />
<input type="text" value="1" id="theValue" runat="server" />
<input type="text" id="FileID" runat="server" value="1" />
<input type="text" id="GetImageURL " runat="server" />
</form>
And Also Create Javascript Function.
// Addcontrol Function Used to Dynamically Create a File field Controls
function addcontrol()
{
var ni=document.get ElementById('my Div');
var numi=document.g etElementById(' theValue');
var num=(document.g etElementById(" theValue").valu e-1)+2;
numi.value=num;
var divname="my"+nu m+"div";
document.getEle mentById("FileI D").value=docum ent.getElementB yId("FileID").v alue+","+num;
var newdiv=document .createElement( 'div');
newdiv.setAttri bute("id",divna me);
newdiv.innerHTM L="<input type='file' id='file_"+num+ "'><input
type='button' onclick=\"addco ntrol()\" value=' + '><input type='button'
onclick=\"remov e('"+divname+ "','"+ num +"')\" value=' - '>";
ni.appendChild( newdiv);
}
// The Following function used to Remove a File field Control
function remove(divname, val)
{
var va=document.get ElementById("Fi leID").value.sp lit(",");
var dd="";
for(i =0;i<va.length; i++)
{
if(va[i]!=val)
{
dd+=va[i]+",";
}
}
if(dd.length>1)
{
dd=dd.substring (0,dd.length-1);
}
document.getEle mentById("FileI D").value=dd ;
var d=document.getE lementById('myD iv');
var olddiv=document .getElementById (divname);
d.removeChild(o lddiv)
}
The Following Function used to Get image URL
function GetImageUrlText ()
{
var getvalues="";
var hval=document.g etElementById(" FileID").value. split(",");
var len=hval.length ;
for(j=0;j<hval. length;j++)
{
getvalues+=docu ment.getElement ById("file_"+hv al[j]).value+",";
}
document.getEle mentById("GetIm ageURL").value= getvalues;
}
Then Go to Aspx.Cs page
You should call javascript function Page Load
btn_submit.Attr ibutes.Add("onc lick", "javascript
:ret urn
GetImageUrlText ()");
The following code you put Button click(Upload Multiple Image)
protected void btn_Click(objec t sender, EventArgs e)
{
Array ImageUrl = GetImageURL.Val ue.Split(',');
for (Int32 i = 0; i < ImageUrl.Length ; i++)
{
if (ImageUrl.GetVa lue(i).ToString () != "")
{
string StrImage = ImageUrl.GetVal ue(i).ToString( );
string StrImageName =
StrImage.Substr ing(StrImage.La stIndexOf("\\") + 1);
file_1.PostedFi le.SaveAs(Serve r.MapPath("~/images/") +
StrImageName);
}
}
}
Please Try ...
Cheers,
Rajagopal.T