How to render remote images through controller action? - c#

In my asp.net mvc3 project I have attachments section where user can upload his confidental images(scanned contracts), which i store on remote server. But when i render attachments i don't want to give full URI of image like
<img href="http://imageURI..." />.
I think i can render them trough controller action like
public ActionResult RenderImage
{
return File(...);
}
but in method File I can't pass URI path.
Can anyone suggest me better solution.

If the client browser is not IE8, just use Data URI Image. Check this out.
#Html.DrawImage("content/images/cat.jpg","funny cat")
Output
<img alt="funny cat" src="data:image/jgp:base64,________" />

Related

Using Response.Redirect with Friendly URLS in ASP.NET

I am designing a website using friendly urls in ASP.NET. I am encountering an interesting bug when I am using response's redirect function with variables. When the page is redirected with a variable, the page you land on seems to think that the page you are on is in fact the home directory, destroying all links in the process. For example, if I redirect to a page and pass a variable like so:
Response.Redirect("nextpage/variable",false);
If I have an image on the next page, the link changes from:
<img src="images/foo.png">
to
<img src="nextpage/images/foo.png">
This happens no matter what the original path is. I have tried to link the image to the home directoty, ie:
<img src="./images/foo.png">
<img src="~/images/foo.png">
Nothing has worked. It now thinks the home directory begins at the new page (I assume because it sees the name of the friendly url and thinks its a folder)
To any of those looking for an answer, the trick is not to use './' or "~/", but simple to use the forward slash '/' , like so:
<img src="/images/foo.png">
The forward slash goes back to the root for all links and images. :)

ASP.NET unable to display image

For reasons outside my control I have been requested to render a series of images on a web page (sort of a gallery view) based on a directory structure utilizing a UNC path. You'll note that I'm using relative paths as I work on this but will be deploying the site using UNC conventions.
I’ve created the following partial view to render the images, I can format later.
#model System.Collections.Generic.List<string>
#foreach (var image in Model)
{
<div id="ptImage">
<img src="#image" alt="#Path.GetFileName(image)"/>
</div>
}
Where #image above represents the absolute path to the image needing to be rendered.
The paths displayed in view source of the page are what I feel I should be expecting back and truly define locally the location of the image. When accessing images via a relative path is this what others would expect?
<div id="gImg">
<img src="C:\Projects\Test_Site\Site_1\ES3\ES3_0.bmp" alt="ES3_0.bmp" />
</div>
<div id="gImg">
<img src=" C:\Projects\Test_Site\Site_2\ES4\ES4_0.jpg" alt="ES4_0.jpg" />
</div>
When the partial view loads I only see the alternative text of the image, not the image itself. A look at the IIS Express log tells me the following:
http://localhost:1348/TestGallery/ 404 0 2 6, This resource lead me to understand that 404 0 2 x seems to indicate the resource isn't found.
With that truly being the path to the file, what is IIS expecting as a valid input to locate the resource? I'm not sure how to phrase the question to perform a further search.
The src paths in the img tags need to be web site URLs, not actual file locations on disk. Your web app should translate the URLs to grab the image from the appropriate location.
One possible solution:
Make a custom entry in your application's RouteConfig.cs to treat all items on a certain URL path dynamically. (In this example it is a separate controller.)
routes.MapRoute(
name: "Images",
url: "image/{*querypath}",
new { controller = "Image", action = "Retrieve" }
);
In the code that handles arbitrary query path, translate the relative URL (in querypath above) to the UNC path you're looking for and serve the image.
return File(UncRootPath + querypath, "image/png");

Gmail doesn't show picture which return from Controller ASP.NET MVC

Currently, I am doing an task that sends email to user which will contain a picture. The picture is returned from 1 controller.
However, when sending an email, the gmail throws a 404 error even I can see the picture by going to url : Controler/ActionMethod ....
In another hand if sending an email with the src in <img> tag hard code which points directly to the picture location in the server, without calling to the controller it works.
For example:
<img src= "mydomain.com/images/pic.jpg"> => this works
<img src= "mydomain.com/ImageController/GetImage"> => this goes to controller then gets image which doesn't work.
becuse GetImage views folder have no images you need to use relative path.
img src= "~/images/pic.jpg" hope it works.

WebBrowser navigate via session cookie in window phone

I am trying to login a website by POST method through HttpWebRequest. In the beginning, i create a portrait page to let user to enter username and password and access the web page. I successfully to Post the data and get the HTML content from the website. Finally I using the Webrowser.NavigateTostring method to display the website in the webBrowser I create.
Problem 1: Inside the webBrowser, I can not do any action in the website. In the normal webBrowser, after successfully login I can do any action such as go to event forum..but what i face is I can't navigate to event forum or any forum.
Problem 2: Inside the webBrowser, the image will not showing eg, user picture, product picture. I also trying to use IsolatedStorage method to show the HTML content but it also non work.
What I confusing is wheather the Webrowser.NavigateTostring method is a way to asynchronous operation to the website in internet? and what I doing wrong? or I looking for wrong direction?
Any help would be appreciated. Thank you.
Unfortunately, Set-Cookie is one of the HTTP headers that are not supported.
This way will suffer from these problems as if you load the HTML using WebBrowser.NavigateToString or from Isolated Storage none of the relative links to images, scripts or CSS will work. Also it is impossible to pass any Cookies from HttpWebRequest to the WebBrowser
The way to do this is to use the WebBrowser control itself to do the POST. There is a overload of the WebBrowser.Navigate method as documented on MSDN here. That will allow you to POST data to your URL.
// generate your form data based on the data you got from your "portrait page"
// and get the bytes from that.
// (e.g. write your post data to a MemoryStream as UTF8 and get its bytes)
byte[] formBytes = ...
// write HTTP headers here, including the type of data you're posting, e.g.:
string headers = "Content-Type: application/x-www-form-urlencoded"
+ Environment.NewLine;
Uri uri = ... // where you want the POST data to be sent
this.webBrowser.Navigate(uri, formBytes, headers);
That way your web browser will be properly initialized and your cookies, images, scripts and CSS should all work.

how to insert image in html action link? asp.net mvc

I have navigation and many link on my webproject from html action links.
They are ugly with underline. I would like to insert some image with name or play with styles of action link.
Is it possible? How to do that?
Thanks and take care,
Ragims
You could use css to remove the underlines or place a backgroundimage,
otherwise you could also just create the link like so:
<img src="yourimg.jpg" />
Html.ActionLink and Url.Action return the same URL. The difference is that the first creates an HTML element while the second returns just the URL to that action.
Another option is to use Url.RouteUrl or Html.RouteLink to create a link based off your route (to an action) instead of directly to an action.
One solution is to create an HtmlHelper extension method for creating an image-specific action link. A detailed tutorial can be found here.
If You are on MVC 3-4 with razor view engine then this may help you-
#Html.ActionLink("your link Name", "Action Method", "Controller",
new { style = "background-image:url('.././Images/imageyouwanttoshow.png')" },null)
Instead of using #Html.ActionLink("linkname","action","controller")
you can use following
<a href='#Url.Action("action", "controller")'>
<img src='#Url.Content("~/images/imageName.png")' />
"images" is my folder for storing the images.
#Url.Content() is to know the path.
You can pass your action and the controller for that action to #Url.Action().
#Url.Action() works similar to the #Html.ActionLink().
Now your link will get replaced by the image.

Categories