Today i will explain you how to upload multiple file in asp.net .
1 . In default.aspx page Create java script In Head Tag.
<script language="javascript" type="text/javascript">
var counter = 1;
var clicks = 0;
function AddFileUpload() {
var div = document.createElement('DIV');
div.innerHTML = '<input id="file' + counter + '" name = "file' + counter + '" type="file"/>'
+ ' ' +
'<input id="Button' + counter + '" type="button" ' + 'value="Remove" onclick = "RemoveFileUpload(this)" />';
document.getElementById("FileUploadContainer").appendChild(div);
counter++;
}
function RemoveFileUpload(div) {
document.getElementById("FileUploadContainer").removeChild(div.parentNode);
counter--;
}
function btnAdd_onclick() {
}
</script>
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<input type="button" id="btnAdd" value="Attach Files"
onclick = "AddFileUpload()" onclick="return btnAdd_onclick()"
style="background-color: #045580; color: #FFFFFF;cursor:pointer;" />
</body>
protected void BtnFileUpload_Click(object sender, EventArgs e)
{
StringBuilder txtdescp = new StringBuilder();
for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFile postedfile = Request.Files[i];
if (postedfile.ContentLength > 0)
{
string filename = DateTime.Now.Ticks + postedfile.FileName;
if ((Path.GetExtension(filename).ToLower() == ".jpg" || Path.GetExtension(filename).ToLower() == ".docx" ||
Path.GetExtension(filename).ToLower() == ".pdf" || Path.GetExtension(filename).ToLower() == ".jpeg" ||
Path.GetExtension(filename).ToLower() == ".xls" || Path.GetExtension(filename).ToLower() == ".gif" ||
Path.GetExtension(filename).ToLower() == ".png" || Path.GetExtension(filename).ToLower() == ".xlsx"))
{
postedfile.SaveAs(Server.MapPath("files/" + filename));
mail.Attachments.Add(new Attachment(Request.PhysicalApplicationPath + "files/" + filename));
}
}
else
{
lblmsg.Text = "File does not support";
}
}
}
out put..
1 . In default.aspx page Create java script In Head Tag.
<script language="javascript" type="text/javascript">
var counter = 1;
var clicks = 0;
function AddFileUpload() {
var div = document.createElement('DIV');
div.innerHTML = '<input id="file' + counter + '" name = "file' + counter + '" type="file"/>'
+ ' ' +
'<input id="Button' + counter + '" type="button" ' + 'value="Remove" onclick = "RemoveFileUpload(this)" />';
document.getElementById("FileUploadContainer").appendChild(div);
counter++;
}
function RemoveFileUpload(div) {
document.getElementById("FileUploadContainer").removeChild(div.parentNode);
counter--;
}
function btnAdd_onclick() {
}
</script>
2 . In default.aspx page Add.
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<input type="button" id="btnAdd" value="Attach Files"
onclick = "AddFileUpload()" onclick="return btnAdd_onclick()"
style="background-color: #045580; color: #FFFFFF;cursor:pointer;" />
</body>
3. In default.aspx.cs
using System.Text;protected void BtnFileUpload_Click(object sender, EventArgs e)
{
StringBuilder txtdescp = new StringBuilder();
for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFile postedfile = Request.Files[i];
if (postedfile.ContentLength > 0)
{
string filename = DateTime.Now.Ticks + postedfile.FileName;
if ((Path.GetExtension(filename).ToLower() == ".jpg" || Path.GetExtension(filename).ToLower() == ".docx" ||
Path.GetExtension(filename).ToLower() == ".pdf" || Path.GetExtension(filename).ToLower() == ".jpeg" ||
Path.GetExtension(filename).ToLower() == ".xls" || Path.GetExtension(filename).ToLower() == ".gif" ||
Path.GetExtension(filename).ToLower() == ".png" || Path.GetExtension(filename).ToLower() == ".xlsx"))
{
postedfile.SaveAs(Server.MapPath("files/" + filename));
mail.Attachments.Add(new Attachment(Request.PhysicalApplicationPath + "files/" + filename));
}
}
else
{
lblmsg.Text = "File does not support";
}
}
}
out put..
No comments:
Post a Comment