Hi…
I want to rename my file name that while I am uploading file in my files folder. Which changes should I do in below code?. New name should be pick from text box.
protected void Upload_Click(object sender, EventArgs e)
{
String strFilename = Path.GetFileName(FileUpload1.FileName);
String StrFilePathName = String.Empty;
String strBaseDir = AppDomain.CurrentDomain.BaseDirectory + "Files";
if (!Directory.Exists(strBaseDir))
{
Directory.CreateDirectory(strBaseDir);
}
StrFilePathName = strBaseDir + "\\" + strFilename;
FileUpload1.SaveAs(StrFilePathName);
if (!FileUpload1.HasFile) return;
Response.Redirect("~/Default2.aspx");
}
Loading
Satish BhatPosted Jul 29, 2011, 12:59 AM
{
if(FileUpload1.HasFile)
{
string fileExtn = Path.GetExtension(FileUpload1.FileName);
String StrFilePathName = String.Empty;
String strBaseDir = AppDomain.CurrentDomain.BaseDirectory +"Files";
if (!Directory.Exists(strBaseDir))
{
Directory.CreateDirectory(strBaseDir);
}
string newFileName = textNewFileName.Text // Without Extn.
StrFilePathName = strBaseDir + "\\" + newFileName + fileExtn;
FileUpload1.SaveAs(StrFilePathName);
Response.Redirect("~/Default2.aspx");
}
}
Jaganathan BantheswaranPosted Jul 29, 2011, 12:55 AM
Instead of this,
StrFilePathName = strBaseDir + "\\" + strFilename;
Change as the below one,
StrFilePathName = strBaseDir + "\\" + yourfilenametobechanged;
yourfilenametobechanged - name of the file which you want.