German Esquivel
German Esquivel

Reputation: 31

rename files to upload to server ASP.NET

I have a problem - I want to upload images to a local directory on my project in ASP.NET C# and I have succeeded.

But now I need to rename the files before uploading them to my local directory.

How I can do this this?

Upvotes: 3

Views: 7805

Answers (3)

Anant Dabhi
Anant Dabhi

Reputation: 11104

its a simple give a new file name when u save it to local

   string newname = "yournewname";
   string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);  
   FileUpload1.SaveAs(Path.Combine(uploadFolder + newname+ extension));

Upvotes: 0

Nudier Mena
Nudier Mena

Reputation: 3274

it will be something like this

 protected void button1_Click(object sender, EventArgs e){

    string directory = Server.MapPath("uploads");
    string fExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);
    string fileName = "newFileName" + fExtension;
    this.FileUpload1.SaveAs(Path.Combine(directory,fileName));

}

Upvotes: 1

Akash KC
Akash KC

Reputation: 16310

You'll not get Rename method directly to rename the filename.....Instead, you can use Move method to act like renaming in following way:

System.IO.File.Move(oldFile, newFile);

OR, You can use Copy method too:

System.IO.File.Copy(oldFile, newFile);
System.IO.File.Delete(oldFile);

Upvotes: 1

Related Questions