locked
rename file RRS feed

  • Question

  • User-1687766116 posted

    hi,

    I want rename file to (time of system) before uploading in fileupload. 

    how i do?

    Please help me

    Thursday, March 5, 2020 11:41 AM

Answers

  • User-719153870 posted

    Hi mehr_83,

    I want rename file to (time of system) before uploading in fileupload. 

    To rename file name for your fileupload control is very simple using the FileUpload.SaveAs(String) Method and change the String in the method dynamically.

    However, you must notice the colon ":" is invalid character for filename which you can refer to Naming Files, Paths, and Namespaces for the invalid list.

    So you have to change the time format, such as turn ":" to "." or any other, or if you prefer datetime.

    Below is a demo you can refer to:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:FileUpload ID="FileUpload1" runat="server" />
                <asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click"/>
            </div>
        </form>
    </body>
    </html>
            protected void Button1_Click(object sender, EventArgs e)
            {
                if (FileUpload1.HasFile)
                {
                    string filename = Server.MapPath("~/") + DateTime.Now.Hour.ToString()+"."+ DateTime.Now.Minute.ToString() + "." + DateTime.Now.Second.ToString()+ Path.GetExtension(FileUpload1.FileName);
                    FileUpload1.SaveAs(filename);
                }
            }

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 6, 2020 1:44 AM

All replies

  • User753101303 posted

    Hi,

    You mean on the client side? You can't for safety reasons. The browser is basically a sandbox that doesn't give any direct access to local resources.

    On the server side you can use the name you want (when using the file system I prefer to keep the file name in the db and use a guid to name the actual file).

    Thursday, March 5, 2020 11:49 AM
  • User-719153870 posted

    Hi mehr_83,

    I want rename file to (time of system) before uploading in fileupload. 

    To rename file name for your fileupload control is very simple using the FileUpload.SaveAs(String) Method and change the String in the method dynamically.

    However, you must notice the colon ":" is invalid character for filename which you can refer to Naming Files, Paths, and Namespaces for the invalid list.

    So you have to change the time format, such as turn ":" to "." or any other, or if you prefer datetime.

    Below is a demo you can refer to:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:FileUpload ID="FileUpload1" runat="server" />
                <asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click"/>
            </div>
        </form>
    </body>
    </html>
            protected void Button1_Click(object sender, EventArgs e)
            {
                if (FileUpload1.HasFile)
                {
                    string filename = Server.MapPath("~/") + DateTime.Now.Hour.ToString()+"."+ DateTime.Now.Minute.ToString() + "." + DateTime.Now.Second.ToString()+ Path.GetExtension(FileUpload1.FileName);
                    FileUpload1.SaveAs(filename);
                }
            }

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 6, 2020 1:44 AM
  • User-1687766116 posted

    thanks a lot, in this code how to get file name after rename?

    Saturday, March 7, 2020 11:16 AM