Hi samsmuthu,
Please refer below sample.
HTML
File Name:
<asp:TextBox ID="txtFileName" Text="SaharaDesert" runat="server" />
<br />
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" Text="Upload" runat="server" OnClick="btnUpload_Click" />
<asp:Label ID="LblDocumnet" runat="server" />
<asp:Label ID="LblFileContentType" runat="server" />
<asp:Label ID="LblFileSize" Text="size" runat="server" />
<asp:Label ID="LblError" runat="server" />
Namespace
C#
using System.IO;
VB.Net
Imports System.IO
Code
C#
protected void btnUpload_Click(object sender, EventArgs e)
{
int count = 0;
string folderName = txtFileName.Text.Replace(" ", "-");
if (!Directory.Exists(Server.MapPath("~/") + folderName))
Directory.CreateDirectory(Server.MapPath("~/") + folderName);
string folderPath = Server.MapPath("~/") + folderName + @"\";
bool fileOK = false;
if (FileUpload1.HasFile)
{
string fileExtension;
fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
string[] allowedExtensions = new[] { ".jpeg", ".png", ".jpg" };
for (int i = 0; i <= allowedExtensions.Length - 1; i++)
{
if (fileExtension == allowedExtensions[i])
fileOK = true;
}
if (fileOK)
{
try
{
if (File.Exists(folderPath + txtFileName.Text.Replace(" ", "-") + fileExtension))
{
string[] files = Directory.GetFiles(folderPath);
foreach (string filePath in files)
{
if (filePath.IndexOf(folderName + "_") != -1)
{
count++;
}
}
string UpFileName = txtFileName.Text.Replace(" ", "-") + "_" + count.ToString() + fileExtension;
FileUpload1.PostedFile.SaveAs(folderPath + UpFileName);
LblDocumnet.Text = UpFileName;
LblFileContentType.Text = FileUpload1.PostedFile.ContentType;
decimal size = Math.Round((System.Convert.ToDecimal(FileUpload1.PostedFile.ContentLength) / (decimal)System.Convert.ToDecimal(1024)), 2);
LblFileSize.Text = size.ToString();
}
else
{
string UpFileName = txtFileName.Text.Replace(" ", "-") + fileExtension;
FileUpload1.PostedFile.SaveAs(folderPath + UpFileName);
LblDocumnet.Text = UpFileName;
LblFileContentType.Text = FileUpload1.PostedFile.ContentType;
decimal size = Math.Round((System.Convert.ToDecimal(FileUpload1.PostedFile.ContentLength) / (decimal)System.Convert.ToDecimal(1024)), 2);
LblFileSize.Text = size.ToString();
}
}
catch (Exception ex)
{
LblError.Text = "File could not be uploaded.";
}
}
else
{
LblError.Text = "Accept only .jpeg .png .jpg only";
LblDocumnet.Text = "";
}
}
}
VB.Net
Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim count As Integer = 0
Dim folderName As String = txtFileName.Text.Replace(" ", "-")
If Not Directory.Exists(Server.MapPath("~/") & folderName) Then
Directory.CreateDirectory(Server.MapPath("~/") & folderName)
End If
Dim folderPath As String = Server.MapPath("~/") & folderName & "\"
Dim fileOK As Boolean = False
If FileUpload1.HasFile Then
Dim fileExtension As String
fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower()
Dim allowedExtensions As String() = {".jpeg", ".png", ".jpg"}
For i As Integer = 0 To allowedExtensions.Length - 1
If fileExtension = allowedExtensions(i) Then
fileOK = True
End If
Next
If fileOK Then
Try
If File.Exists(folderPath & txtFileName.Text.Replace(" ", "-") & fileExtension) Then
Dim files As String() = Directory.GetFiles(folderPath)
For Each filePath As String In files
If filePath.IndexOf(folderName & "_") <> -1 Then
count += 1
End If
Next
Dim UpFileName As String = txtFileName.Text.Replace(" ", "-") & "_" & count.ToString() & fileExtension
FileUpload1.PostedFile.SaveAs(folderPath & UpFileName)
LblDocumnet.Text = UpFileName
LblFileContentType.Text = FileUpload1.PostedFile.ContentType
Dim size As Decimal = Math.Round((CDec(FileUpload1.PostedFile.ContentLength) / CDec(1024)), 2)
LblFileSize.Text = size
Else
Dim UpFileName As String = txtFileName.Text.Replace(" ", "-") & fileExtension
FileUpload1.PostedFile.SaveAs(folderPath & UpFileName)
LblDocumnet.Text = UpFileName
LblFileContentType.Text = FileUpload1.PostedFile.ContentType
Dim size As Decimal = Math.Round((CDec(FileUpload1.PostedFile.ContentLength) / CDec(1024)), 2)
LblFileSize.Text = size
End If
Catch ex As Exception
LblError.Text = "File could not be uploaded."
End Try
Else
LblError.Text = "Accept only .jpeg .png .jpg only"
LblDocumnet.Text = ""
End If
End If
End Sub
Screenshot