0

Hi all..
I'm having a little trouble with an upload of a file..
i use asp.net with C# language..
I want to add datetime in the name of the file so that when a user uploads a file such as "test.doc" it would be saved into the server as "test-20101123-0533-milisecond.doc" and stored in the database as in a field as Uploads/test-20101123-0533-milisecond.doc

"can u help me, please?"

2
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by kieky
0

Hi,heres some sample code that i hope will help you,basically whats happening with this code is that we retrieve,the FileName from an Upload Control then we separate the fileName & Extension,what youb then do is build up the file Name with fileName+DateTime+Extension,then we save the file on the specied server path,this is not complicated code its just show the basics,if you have futher question feel free to post them...

protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.FileUpload1.HasFile)
            {
          
                string strFileName = FileUpload1.FileName.ToString();
                string strExtension = Path.GetExtension(strFileName);
                string strTimeStamp = DateTime.Now.Date.ToString();
                strTimeStamp = strTimeStamp.Replace("/", "-");
                strTimeStamp = strTimeStamp.Replace(" ", "-");
                strTimeStamp = strTimeStamp.Replace(":", "-");
                string strName = Path.GetFileNameWithoutExtension(strFileName);
                strFileName = strName + "-" + strTimeStamp + strExtension;
                txtName.Text = strTimeStamp;
                this.FileUpload1.SaveAs(Server.MapPath(@"~\Uploads\" + strFileName.ToString()));
            }
            else
            {
               
            }
        }
0

Sorry forgot this part. Note that the above code is part of the IO class so you need to put the following on top.

using System.IO;

Edited by mike_2000_17: Fixed formatting

0

Heres the Refined Code

if (this.FileUpload1.HasFile)
            {
          
                string strFileName = FileUpload1.FileName.ToString();
                string strName = Path.GetFileNameWithoutExtension(strFileName);
                string strTimeStamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                string strExtension = Path.GetExtension(strFileName);
                strTimeStamp = strTimeStamp.Replace(":","");
                strTimeStamp = strTimeStamp.Replace("-","");
                strTimeStamp = strTimeStamp.Replace(" ","");
                strTimeStamp = strTimeStamp.Insert(8,"-");
                strTimeStamp = strTimeStamp.Insert(13, "-");
                strFileName = strName + "-" + strTimeStamp + strExtension;
                txtName.Text = strFileName;
                this.FileUpload1.SaveAs(Server.MapPath(@"~\Uploads\" + strFileName.ToString()));
            }
            else
            {
                ////MessageBox Logic
            }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.