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?"

Recommended Answers

All 5 Replies

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
            {
               
            }
        }

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;

to samacoba.. thank you very much..
very helpful.. :)

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
            }

thx for this.. very helpful.. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.