locked
WebImage image rename RRS feed

  • Question

  • User97660826 posted

    Hi guys,

    Need some help renaming an image to "userid_ID.jpg" - userid being a variable based on which user is logged in.

    I currently have the GUID way - but I want the name to have more meaning - so that I can call the image at a later stage as required.

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



    I know the below is wrong - but I suppose the logic would be like:   

    Thanks,

    photo.FileName = @WebSecurity.CurrentUserId + "_ID"; 
    
    Thursday, October 30, 2014 9:16 AM

Answers

  • User-821857111 posted

    The file will take whatever name you provide in the Save method:

    var photo = WebImage.GetImageFromRequest();
    if(photo != null){
        photo.Save(Server.MapPath("~/images/" + Guid.NewGuid().ToString() + "-" + WebSecurity.CurrentUserId + Path.GetExtension(photo.FileName));
    }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 30, 2014 9:28 AM
  • User-735851359 posted

    Hello,

    If you do not have guid file names just remove that from the Mike's code:

    var photo = WebImage.GetImageFromRequest();
    if(photo != null){
        photo.Save(Server.MapPath("~/images/" + WebSecurity.CurrentUserId + Path.GetExtension(photo.FileName));
    }

    I hope it helps..

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 30, 2014 4:10 PM

All replies

  • User-821857111 posted

    The file will take whatever name you provide in the Save method:

    var photo = WebImage.GetImageFromRequest();
    if(photo != null){
        photo.Save(Server.MapPath("~/images/" + Guid.NewGuid().ToString() + "-" + WebSecurity.CurrentUserId + Path.GetExtension(photo.FileName));
    }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 30, 2014 9:28 AM
  • User97660826 posted

    HI Mike,

    Many thanks for the quick reply.

    I'm checking it out - will post back shortly.

    Thanks

    Thursday, October 30, 2014 10:09 AM
  • User97660826 posted

    Didn't get it working yet  - 

    Image still saves as guid file name only - i'm sure i'm missing something but will check it out a bit further tonight.

    Thursday, October 30, 2014 11:05 AM
  • User-735851359 posted

    Hello,

    If you do not have guid file names just remove that from the Mike's code:

    var photo = WebImage.GetImageFromRequest();
    if(photo != null){
        photo.Save(Server.MapPath("~/images/" + WebSecurity.CurrentUserId + Path.GetExtension(photo.FileName));
    }

    I hope it helps..

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 30, 2014 4:10 PM
  • User97660826 posted

    Hi,

    I overlooked it yesterday (multi-tasking) - it's working 100% now.

    Thanks again for the quick assistance Mike/ayanmesut.

    much appreciated.

    Friday, October 31, 2014 1:42 AM
  • User-821857111 posted

    If you do not have guid file names just remove that from the Mike's code:

    A possible problem with that approach is that you can only have one file per user. It's not a problem if the user is only permitted one image, such as one to be used as an avatar.

    Friday, October 31, 2014 3:14 AM
  • User97660826 posted

    Thanks Mike,

    In this case each user will only ever have 2 images - My plan is keep the images unique by using the Websecurity.CurrentUserID and appending "ID" and "POD" respectively to the 2 uploaded images. if a second "POD" is uploaded - the code should check for an existing POD and then delete or overwrite the 'old' image. ( have to still put the code in for this part).

    Cheers

    Sunday, November 2, 2014 3:02 AM
  • User-821857111 posted

    if a second "POD" is uploaded - the code should check for an existing POD and then delete or overwrite the 'old' image. ( have to still put the code in for this part).

    No you don't. If you save a file with the same name as an existing file, it will happily overwrite the existing file.

    Sunday, November 2, 2014 3:13 AM