Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends,

I have provided a option for the user to upload a file.
Where as he/she will be uploading the file with any name.
But while storing it in a folder I have to rename the file with respective User's ID.
I want to do this in C#, Kindly guide me.
Posted
Updated 24-Jul-22 10:48am

Hi,
It is very easy to do. When the file is uploaded, get the extension of the file and save it as with your desired name. Please check the sample code below. Let Uploader be your fileupload control ID.

C#
if (Uploader.HasFile)
        {
            string extension = Path.GetExtension(Uploader.PostedFile.FileName);
            Uploader.SaveAs(uploadFolder + "NewFileName"+ extension);
            labelfile.Text = "File uploaded successfully as: " + "NewFileName"+ extension;
        }



Hope it helps.. Happy coding.. :)
 
Share this answer
 
Comments
Arunprasath Natarajan 10-Oct-12 3:32am    
Tan q, It means a lot
Hi you can achieve this while uploadin to your folder -


by this you can make your file name unique and with your username

C#
if (ASPxUploadControl1.HasFile ) or//(Fileupload.Hasfile)
 {
     try
     {
         string extension = Path.GetExtension(ASPxUploadControl1.FileName);
         string id = Guid.NewGuid().ToString();  //here give your username or whatever you want
         string fileLocation = string.Format("{0}/{1}{2}", Server.MapPath("upload/"), id, extension);
         ASPxUploadControl1.SaveAs( fileLocation );
         StatusLabel.Text = "Upload status: File uploaded!";
     }
     catch (Exception ex)
     {
         StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
     }
 }
 
Share this answer
 
 
Share this answer
 
try this function to upload file

public static string UploadFile(FileUpload File1)
{

Randoms r = new Randoms();
string random = r.Voucher();

string files = "";if (File1.PostedFile != null)
{

string StrFileName = File1.PostedFile.FileName.Substring(File1.PostedFile.FileName.LastIndexOf("\\") + 1);

string StrFileType = File1.PostedFile.ContentType;
int IntFileSize = File1.PostedFile.ContentLength;

if (IntFileSize > 0)
File1.PostedFile.SaveAs(HttpContext.Current.Server.MapPath("Files/" + random + DateTime.UtcNow.DayOfYear.ToString() + StrFileName));

files = (("Files/" + random + DateTime.UtcNow.DayOfYear.ToString() + StrFileName));
}

return files;
}

 

and make a class named  Randoms to change file name when uploading

this is random class

Randoms.cs

=========

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

using System.Text;

using System.IO;

/// <summary>
/// Summary description for Randoms

/// </summary>

public class Randoms

{

private int RandomNumber(int min, int max)
{

Random random = new Random();return random.Next(min, max);
}

protected string RandomString(int size, bool lowerCase)
{

StringBuilder builder = new StringBuilder();

Random random = new Random();
char ch;

for (int i = 0; i < size; i++)
{

ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);

}

if (lowerCase)
return builder.ToString().ToLower();

return builder.ToString();
}

 

protected string RandomString2(int sizes, bool lowerCases)
{

StringBuilder builders = new StringBuilder();
Random randoms = new Random();

char chs;for (int i = 0; i < sizes; i++)
{

chs = Convert.ToChar(Convert.ToInt32(Math.Floor(25 * randoms.NextDouble() + 65)));
builders.Append(chs);

}

if (lowerCases)
return builders.ToString().ToLower();

return builders.ToString();
}

}

 

 

and to call the function

 

 

string filelocation = UploadFile(FileUpload1);

hope this helps
 
Share this answer
 
Comments
CHill60 25-Jul-22 6:58am    
OP did not ask for code to upload a file 10 years ago but how to rename it with respect to specific criteria, not some random content.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900