Reputation: 5597
I am using following function to display byte[] array image in asp.net mvc
public ActionResult Images(int id)
{
byte[] imageData = GetImage(id);
string contentType = "image/jpeg";
return File(imageData, contentType);
}
<img src="/Home/Images/2">
So Problem is this i don't want to show /Home/Images/2 . I want to show any random name with image format like this
> /Home/Images/imgnameetc.jpg
Note: I have image just byte[] array form.No any other information like name ,image format
I did search on it but i can't found any solution
Please help.
Upvotes: 0
Views: 317
Reputation: 5989
Take a look at following URL, You can achieve this with the help of MVC Routers. Just add one router specific to your request and configure your IIS for it.
Upvotes: 0
Reputation: 2487
The route table must be configured to accept an image's name in the URL. And wherever you are storing the image, you can store the name along with it. A separate column incase of database.
In register routes method of Global.asax, try writing this.
routes.MapRoute(
"ImageRoute", // Route name
"{controller}/{action}/{imageName}", // URL with parameters
new { controller = "Home", action = "Images", imageName = UrlParameter.Optional });
EX url = localhost/Home/Images/image1.jpg.
Upvotes: 0
Reputation: 1064204
Your server-side code can't spoof the url that the user navigated to. If they navigated to /Home/Images/2
, then that is the url. What you can do is have them navigate to /Home/Images/imgnameetc.jpg
instead of /Home/Images/2
, but then you will need to configure the routes to allow names instead of an id, and have somewhere where you can lookup the details from a name (/slug) instead of from an id. For example, you might have a UrlSlug
column in your database which is indexed (I would suggest unique and non-clustered, with a restriction to non-null values, and including the id as an additional included value (not part of the index, but available for slug => id queries) which contains imgnameetc.jpg
for that record, so you can find it with a simple where
test.
One other thing you can do is set the content-disposition header, which tells the browser how the file identifies itself; for example, setting the "content-disposition"
header to "inline;filename=imgnameetc.jpg"
may help if the user tries to save the file; however, it will not change the url shown in the browser.
Upvotes: 1