locked
Save an image with a different name RRS feed

  • Question

  • User-1753299103 posted

    I need to tottaly change the name of uploaded file but with its orginal extension.

    The following won't do:

    @{ WebImage photo = null;
    var newFileName = "";
    var imagePath = "";
    if(IsPost){
    photo = WebImage.GetImageFromRequest();
    if(photo != null){
    newFileName = Guid.NewGuid().ToString() + "_" + Path.GetFileName(photo.FileName);
    imagePath = @"images\" + newFileName;
    photo.Save(@"~\" + imagePath);
    }
    }
    }

    Wednesday, July 3, 2013 9:17 PM

Answers

  • User-1716253493 posted

    Path.GetExtension(photo.FileName);

    newFileName = Guid.NewGuid().ToString() + "_abcdefg" + Path.GetExtension(photo.FileName);



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 3, 2013 9:37 PM
  • User895691971 posted

    Actually the code 

    photo.FileName

    Handles the name, so the extension is also provided in this! I mean if the file was 

    C:\Users\Afzaal\Pictures\Folder\profile pic.png

    Then the FileName will have the value as "profile pic.png"

    So writing the line as 

    Guid.NewGuid().ToString() + "_" + Path.GetFileName(photo.FileName);

    Will let you have the Image file name as: 45ea4527-7ddd-4965-b9ca-c6444982b342_profile pic.png


    If you don't get the required result, then there is a bug in your code, please check it twice!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 7, 2013 11:35 AM

All replies

  • User-1716253493 posted

    Path.GetExtension(photo.FileName);

    newFileName = Guid.NewGuid().ToString() + "_abcdefg" + Path.GetExtension(photo.FileName);



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 3, 2013 9:37 PM
  • User-1753299103 posted

    Firstly, please show me as to how you would save the new file to a location?

    Are you aware that webmatrix upload helper picks the true extension of the actual file and not what has been specified in the Request.File

    That's why, if the extension of a file has been manually changed lets say from .jpg to .gif then Path.GetExtension(photo.FileName) woud be .gif But it really saves it with .jpg

    And then you would have a different name between what has been uploaded and what has been called in the database.

     

    Wednesday, July 3, 2013 9:53 PM
  • User895691971 posted

    Actually the code 

    photo.FileName

    Handles the name, so the extension is also provided in this! I mean if the file was 

    C:\Users\Afzaal\Pictures\Folder\profile pic.png

    Then the FileName will have the value as "profile pic.png"

    So writing the line as 

    Guid.NewGuid().ToString() + "_" + Path.GetFileName(photo.FileName);

    Will let you have the Image file name as: 45ea4527-7ddd-4965-b9ca-c6444982b342_profile pic.png


    If you don't get the required result, then there is a bug in your code, please check it twice!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 7, 2013 11:35 AM