Rename file name during uploading in Asp .Net

 In this article we will see how to rename file name during uploading in asp .net. Renaming of files is really important when you want to upload your files using your custom names. For this article we will use  FileUpload control to upload files and inbuild .Net methods to rename and upload file.

HTML:


        

CodeBehind

protected void btnUploadFile_Click(object sender, EventArgs e)
    {
        //==== Add Namespace System.IO

        //==== Get file name without its extension.
        string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(flUpload.FileName);

        //==== Get file extension.
        string fileExtension = Path.GetExtension(flUpload.FileName);

        //=== Now we have both file name and its extension seperately we can now eaisly rename file name.

        //===== Adding some text in begining and end of the file name
        fileNameWithoutExtension = "AddedText-" + fileNameWithoutExtension + "-AddedText";

        //===== Now lets upload the renamed file.

        flUpload.PostedFile.SaveAs(Server.MapPath("/"+ fileNameWithoutExtension + fileExtension));




    }

 

 
About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2024 CodingFusion
50+ C# Programs for beginners to practice