[RESOLVED]WebImage image rename

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

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));
}

HI Mike,

Many thanks for the quick reply.

I’m checking it out – will post back shortly.

Thanks

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.

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..

Hi,

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

Thanks again for the quick assistance Mike/ayanmesut.

much appreciated.

ayanmesut

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.

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

nouxoid

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.

Leave a Reply