0% found this document useful (0 votes)
0 views

assignment_aspx_cs

The document is a C# ASP.NET code for a web page that allows users to upload files, specifically resumes, with validation for file types and size. It checks for allowed file extensions, prevents duplicate file uploads, and provides user feedback through labels. Additionally, it binds the uploaded files to a data list for display on the page.

Uploaded by

siddhupansari
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

assignment_aspx_cs

The document is a C# ASP.NET code for a web page that allows users to upload files, specifically resumes, with validation for file types and size. It checks for allowed file extensions, prevents duplicate file uploads, and provides user feedback through labels. Additionally, it binds the uploaded files to a data list for display on the page.

Uploaded by

siddhupansari
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

using System;

using System.IO;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Assignment : System.Web.UI.Page


{
protected void Page_Load(object sender, EventArgs e)
{

protected void Button2_Click(object sender, EventArgs e)


{
// Log or debug statement to check if this method is called
System.Diagnostics.Debug.WriteLine("Button clicked!");

if (FileUpload1.HasFile)
{
try
{
// Define the folder path where files will be saved
string folderPath = @"C:\MONCY\Proji\Resumes";

// Get the file name


string fileName = Path.GetFileName(FileUpload1.FileName);

// Get the file extension and convert it to lowercase


string fileExtension = Path.GetExtension(fileName).ToLower();

// Define allowed file extensions


string[] allowedExtensions = { ".pdf", ".doc", ".docx", ".txt" };

// Validate the file extension


if (!allowedExtensions.Contains(fileExtension))
{
Label5.Text = "Error: Only PDF, DOC, DOCX, and TXT files are
allowed.";
Label5.Visible = true;
return; // Exit the method if the file type is not allowed
}

// Combine path and filename


string fullPath = Path.Combine(folderPath, fileName);

// Check for duplicate files


if (File.Exists(fullPath))
{
Label5.Text = "Error: A file with this name already exists.";
Label5.Visible = true;
return; // Exit the method if the file exists
}

// Save the file


FileUpload1.SaveAs(fullPath);

// Display success message


Label5.Text = "Submitted successfully!";
Label5.Visible = true; // Make label visible
}
catch (Exception ex)
{
Label5.Text = "Error: " + ex.Message;
Label5.Visible = true; // Make label visible
}
}
else
{
Label5.Text = "Please select a file to upload.";
Label5.Visible = true; // Make label visible
}
}
}

protected void Page_PreRenderComplete(object sender, EventArgs e)


{
System.IO.DirectoryInfo di = new
System.IO.DirectoryInfo(Server.MapPath("~/resumes"));
DataList1.DataSource = di.GetFiles();
DataList1.DataBind();
}

protected void Button2_Click(object sender, EventArgs e)


{
if (FileUpload1.HasFile)
{
if (FileUpload1.FileContent.Length < 10000)
{
FileUpload1.SaveAs(Server.MapPath("~/resumes/" +
FileUpload1.FileName));
}
}

You might also like