[RESOLVED]Specifying save as filename when using ASP.Net WebImage helper

I have created a method that uses the WebImage helper to allow me to disaply images with dynamic sizes and everything is working fine except when you try save the file the file name is not what I set it. If a user goes to /Games/Images/Character/Warrior.jpg/500/500
when the users tries to save the file the default name is 500.jpg but I want it to be Warrior.jpg I am setting the WebImage.FileName property to Warriro.jpg so I am not sure what I am doing wrong.

[Route("{FileType}/{SectionName}/{FolderName}/{FileName}/{Width}/{Height}")]
public void GetImage(string FileType, string SectionName, string FolderName, string FileName, Int16 Width, Int16 Height)
{
    string Path = FileType + @"" + SectionName + @"" + FolderName + @"" + FileName;
    WebImage Image = new WebImage(Path);
    Image.Resize(Width, Height, true);
    Image.FileName = FileName;
    Image.Write();
}

do you mean that the user right clicks the image displayed in their browser to save it and that’s where they’re getting the name 500.jpg?

I’d restructure the route such that  Filename comes last

[Route("{FileType}/{SectionName}/{FolderName}/{Width}/{Height}/{FileName}")]

Then when your user goes to the image, it will be :

/Games/Images/Character/500/500/Warrior.jpg

The browser should allow saving as the correct filename in that case.

mbanavige

do you mean that the user right clicks the image displayed in their browser to save it and that’s where they’re getting the name 500.jpg?

I’d restructure the route such that  Filename comes last

[Route("{FileType}/{SectionName}/{FolderName}/{Width}/{Height}/{FileName}")]

Then when your user goes to the image, it will be :

/Games/Images/Character/500/500/Warrior.jpg

The browser should allow saving as the correct filename in that case.

That is correct, when the user right clicks and choses to save. I have actually tried your suggestion but the problem is that MVC tries to route it to a physical file and gives me a 404 error. I originally was trying to deal with that over at

http://forums.asp.net/t/2012803.aspx?Map+route+with+File+Extention+to+Action+in+controller
+ but have not had any success, so now I am trying to do it this way. If I can get it working either way I will be happy as it is the last problem with this module

hmmm…  when you had a trailing 500 with no extension, there was no attempt to route to a physical file and the browser save-as was giving you a filename with an extension. 

what if you change the route as mentioned above but just use this url:

/Games/Images/Character/500/500/Warrior

you’d need to add the jpg extension back on in your code (assuming it’s always a .jpg)

My goal it to avoid having to drop the .jpg extension. This module will be going to a specific sub-domain to serve up many types of files, lots of which already have links to the physical files on a few of my websites. In  order to drop the extension I would
need to go into multiple projects and modify the controllers to strip out extensions and pass them in another way then deal with the reconstruction.

My understanding of the WebImage helper is that the WebImage.FileName is supposed to set the file name so I am not sure if I am missunderstaning it, it is broke, or it just does not apply to web interfaces

The browser has no idea what the filename is for the file that exists on your server.  The browser only see the url that was used to deliver the content and it will assume that the last bit of that url is the filename.  So to get the browser to behave the
way you want, you do need to give it a filename it can see on the end of the url.

your routing issue then is that the iis static file support is kicking in prior to your route because it also sees the filename on the end of the url and so iis thinks it’s a static file.

A possible solution (with caveats) might be found here: 

http://www.hanselman.com/blog/BackToBasicsDynamicImageGenerationASPNETControllersRoutingIHttpHandlersAndRunAllManagedModulesForAllRequests.aspx

Leave a Reply