How to Rename file using <asp:File Upload?

Last post 10-02-2009 4:37 AM by kipo. 8 replies.

Sort Posts:

  • How to Rename file using <asp:File Upload?

    09-26-2008, 3:35 AM
    • Contributor
      2,320 point Contributor
    • susain
    • Member since 10-29-2007, 11:51 AM
    • Hyderabad, Andhra Pradesh, India
    • Posts 641

    Hi Friends,

    I am using <asp:File Uplaod control.

    Requirement:

    If I select any file from the local machine, I need to rename same file name if file contains spaces in between the file name.

    Example: Test 2.jpg so I need to rename and save it as Test2.jpg in the folder.

    How to achieve this?

    Need help

     

    Thanks,

    Linked In: http://www.linkedin.com/in/mohammedfarooq123

    Mark as answer if this post help full to you.
  • Re: How to Rename file using <asp:File Upload?

    09-26-2008, 4:29 AM
    • Star
      13,680 point Star
    • kipo
    • Member since 07-20-2006, 7:10 AM
    • Croatia
    • Posts 2,346

    Try with this:

            if (FileUpload1.HasFile)
            {
                FileUpload1.SaveAs(MapPath("~/uploadFolder/" + FileUpload1.FileName.Replace(" ", "")));
            }

  • Re: How to Rename file using <asp:File Upload?

    09-26-2008, 9:02 AM
    • All-Star
      15,183 point All-Star
    • yasserzaid
    • Member since 09-22-2007, 9:10 PM
    • Egypt
    • Posts 2,571

    Hi

    try this example to upload image to Album folder in root of website

    and rename image also

            if (FileUpload1.HasFile == true && FileUpload1.PostedFile != null)
            {
                int _size = 9437184;// equal 9 mb
                string _fileExt = System.IO.Path.GetExtension(FileUpload1.FileName);
                if (_fileExt.ToLower() == ".gif" || _fileExt.ToLower() == ".jpeg" || _fileExt.ToLower() == ".jpg")
                {
                    if (FileUpload1.PostedFile.ContentLength <= _size)
                    {
                        string AdsFile = Guid.NewGuid().ToString() + Path.GetExtension(FileUpload1.FileName);
                        FileUpload1.SaveAs(Request.PhysicalApplicationPath + "Album\\" + AdsFile);

                        
                    }
                    else
                    {
                        FileUpload1.Focus();
                        ClientScript.RegisterStartupScript(Type.GetType("System.String"), "messagebox", "<script type=\"text/javascript\">alert('Max file size is 10 m.b');</script>");
                    }
                }
                else
                {
                    FileUpload1.Focus();
                    ClientScript.RegisterStartupScript(Type.GetType("System.String"), "messagebox", "<script type=\"text/javascript\">alert('Only GIF or JPEGare allowed');</script>");
                }
            }
            else
            {
                FileUpload1.Focus();
                ClientScript.RegisterStartupScript(Type.GetType("System.String"), "messagebox", "<script type=\"text/javascript\">alert('select image');</script>");
            }
            

        Good Luck

    Yasser Zaid
  • Re: How to Rename file using <asp:File Upload?

    09-28-2008, 11:09 PM
    • Contributor
      2,320 point Contributor
    • susain
    • Member since 10-29-2007, 11:51 AM
    • Hyderabad, Andhra Pradesh, India
    • Posts 641

    Hi Yasser,

    I tried your sample code, its working fine

    I have uploaded the image file with name(Early to Rise.jpg) so I got the name as d51f7fce-8736-4e8f-a426-86403986840c.jpg

    But to rename the file say EarlytoRise.jpg or for Test 1.jpg to Test1.jpg

    i.e.,, I need to rename the files by removing spaces between the names of the image files.

     

    Thanks,

     

    Linked In: http://www.linkedin.com/in/mohammedfarooq123

    Mark as answer if this post help full to you.
  • Re: How to Rename file using <asp:File Upload?

    09-28-2008, 11:51 PM
    Answer
    • Member
      750 point Member
    • akhilrajau
    • Member since 06-03-2008, 2:16 PM
    • India
    • Posts 192

     You can simply use like kipo gave.

     FileUpload1.SaveAs(Server.MapPath("~/Folder/" + FileUpload1.FileName.Replace(" ", "")));

    Regards,

    Akhil Raj K R
    Esquire Scientific.

    Please Mark as Answer if it helps u

  • Re: How to Rename file using <asp:File Upload?

    09-29-2008, 3:01 AM
    • Participant
      1,069 point Participant
    • elegantkvc
    • Member since 07-30-2007, 4:20 AM
    • chennai
    • Posts 247

    hi,

    you can simple use the Trim function to cut out the blank spaces in a word.

    cheers

     

     

    vijay
  • Re: How to Rename file using <asp:File Upload?

    09-26-2009, 6:25 AM
    • Member
      4 point Member
    • Alagirip
    • Member since 09-08-2009, 3:37 PM
    • Posts 2

    Hai friend,

     i have tried this solution and  got the Result Thanks friend

  • Re: How to Rename file using <asp:File Upload?

    10-02-2009, 3:50 AM
    • Member
      2 point Member
    • sarojladdha
    • Member since 10-02-2009, 7:46 AM
    • Posts 1

      

  • Re: How to Rename file using <asp:File Upload?

    10-02-2009, 4:37 AM
    • Star
      13,680 point Star
    • kipo
    • Member since 07-20-2006, 7:10 AM
    • Croatia
    • Posts 2,346

    sarojladdha:

      

    You'll have to upload that image somewhere (you can use ImageShack: http://imageshack.us/) and than put it here.

Page 1 of 1 (9 items)