3/02/2015

How to store uploaded files into folders organized by year, month, and day in ASP.NET web form?

 

How to store uploaded files into folders organized by year, month, and day in ASP.NET web form?
How to store uploaded files into folders organized by year, month, and day in ASP.NET web form?

protected void btnUpload_Click(object sender, EventArgs e)
    {
        // Get the base directory path where you want to store the files
        string baseDirectory = Server.MapPath("~/uploads/");

        // Get the current date
        DateTime currentDate = DateTime.Now;

        // Get the year, month, and day as numeric values
        string year = currentDate.Year.ToString();
        string month = currentDate.Month.ToString();
        string day = currentDate.Day.ToString();

        // Create the year folder path by combining the base directory and the year
        string yearFolderPath = Path.Combine(baseDirectory, year);

        // Create the month folder path by combining the year folder path and the month
        string monthFolderPath = Path.Combine(yearFolderPath, month);

        // Create the day folder path by combining the month folder path and the day
        string dayFolderPath = Path.Combine(monthFolderPath, day);

        // Create the year folder if it doesn't exist
        if (!Directory.Exists(yearFolderPath))
        {
            Directory.CreateDirectory(yearFolderPath);
        }

        // Create the month folder if it doesn't exist
        if (!Directory.Exists(monthFolderPath))
        {
            Directory.CreateDirectory(monthFolderPath);
        }

        // Create the day folder if it doesn't exist
        if (!Directory.Exists(dayFolderPath))
        {
            Directory.CreateDirectory(dayFolderPath);
        }

        // Get the uploaded file
        if (fileUpload.HasFile)
        {
            // Get the file extension
            string fileExtension = Path.GetExtension(fileUpload.FileName);

            // Generate a unique file name using a combination of current date and time
            string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + fileExtension;

            // Set the file path by combining the day folder path and the generated file name
            string filePath = Path.Combine(dayFolderPath, fileName);

            // Save the file to the specified path
            fileUpload.SaveAs(filePath);

            // Display a success message
            lblMessage.Text = "File uploaded successfully!";
        }
        else
        {
            // Display an error message if no file is selected
            lblMessage.Text = "Please select a file to upload!";
        }
    }

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style>
        .container {
            width: 400px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f2f2f2;
            border: 1px solid #ccc;
            border-radius: 5px;
        }

        .form-group {
            margin-bottom: 10px;
        }

        .form-group label {
            display: block;
            font-weight: bold;
        }

        .form-group input[type="file"] {
            display: block;
            margin-top: 5px;
        }

        .btn-upload {
            display: block;
            margin-top: 10px;
            padding: 5px 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }

        .message {
            margin-top: 10px;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
     <div class="container">
            <div class="form-group">
                <label for="fileUpload">Select File:</label>
                <asp:FileUpload ID="fileUpload" runat="server" />
            </div>
         <asp:Button ID="btnUpload" runat="server" Text="Upload" CssClass="btn-upload" OnClick="btnUpload_Click"/>
            
            <div class="message">
                <asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>
            </div>
        </div>
    </form>
</body>
</html>

Hint: The code uses the DateTime.Now function to get the current date. It then extracts the year, month, and day as numeric values. The code creates a folder structure using these values, starting with a base directory path. It checks if the year, month, and day folders already exist and creates them if they don't. The uploaded file is saved into the corresponding day folder with a unique file name generated using the current date and time.The code uses the DateTime.Now function to get the current date. It then extracts the year, month, and day as numeric values. The code creates a folder structure using these values, starting with a base directory path. It checks if the year, month, and day folders already exist and creates them if they don't. The uploaded file is saved into the corresponding day folder with a unique file name generated using the current date and time.