Image not getting displayed in ASP.NET in local host - c#

There is an image in Images folder in my solution and I have an image controller on my page. and on a button click I set the URL of the image like this,
imgDisp.ImageUrl = #"~/Images/American.jpg";
But the image is not showing up on the browser when I run the code. The image is present in the specified folder and I didnt mention any URL in tag either. What am I missing ? The image URL should be mentioned the above way when running in local host too or should it be done differently ? Thanks in advance for the reply.

Related

How can ı display image in server side

My server url is something like 192.1.1.1:123 and my images folder in server is F:\images\1234.jpg but I published the website in C:\inetpub\wwwroot\
How can I refer to images from F:?
consider using virtual directory
<application path="/files">
<virtualDirectory path="/Images" physicalPath="F:\images" />
</application>
then in browser accessing images from 192.1.1.1:123/files/images/1234.jpg
Mode - 1
Read the image file in browser.
Implement this way in your code file.
ex.~/Content/images/img1.png
Mode - 2
After publish the code in this URL 192.1.1.1:123 then after follow the below step.
Run URL in browser.
Right click image and open in new tab then copy the URL. ex. //192.1.1.1:123/Content/images/img1.png
Paste the URL in the code you need.

Downloading from a URL gives an aspx page instead of the actual file

I am trying to use Selenium to open a URL in Chrome and hit a button, which will download a text file for me. But it's giving me an aspx page instead of the text file.
It's working fine if I do this manually in Chrome.
I also tried to do this with a Firefox driver but I got the same issue.
I am opening the url using driver.Navigate().GoToUrl().
Can you guys please help me on this?

Not showing the images in live server which is showing in my local host

I am working on the Mailbox Module in VS2010 with C#
I have made a control with ascx page and in that page I have created Three different gridviews for Inbox,SentItems and Deleted Items.
In all the three gridviews I have added two columns for the Images for Reply and Forward messages which is shown where the user reply or forward the message.
The functionality works perfectly in my localhost.
But today i have deployed it on live server.
Where I found that I does not showing the images.
When I inspected elements through firebug I found in the HTML source code that it shows me that the "Failed To Load The Given URL" while for other images it shows the images.
Can Anyone tell me why is it happening and help me..?
Check if the url is pointing to the correct physical path on live sever.
Refer this - ASP.NET Web Project Paths
ASP.NET resolves the ~ operator to the root of the current application. You can use the ~ operator in conjunction with folders to specify a path that is based on the current root.
The following example shows the ~ operator used to specify a root-relative path for an image when using the Image server control In this example, the image file is read from the Images folder that is located directly under the root of the Web application, regardless of where in the Web site the page is located.
<asp:image runat="server" id="Image1"
ImageUrl="~/Images/SampleImage.jpg" />
You can use the ~ operator in any path-related property in server controls. The ~ operator is recognized only for server controls and in server code. You cannot use the ~ operator for client elements.
I found solution of this error.
Basically in Help desk set permision red and write where u hosting website. then solve thi solution.
2nd thing: u check folder image folder path where u save image in Help desk

Images don't appear in emails

I have a HTML email template in the App_Data folder of my MVC application. In my code, I use this template to send HTML emails to users. This template references a few images in a folder in my project. The issue is that these images don't appear at all when the user receives the email. I have tried to reference the images using ~/path to image/image.gif. I have tried using ../../path to image/image.gif and I have copied the images to the App_Data folder and just referenced the images thus image.gif. Nothing is working. Does anyone have any suggestions?
the images either need to be stored in a publicly accessible location with a full reference to the image <img src="http://my.domain.com/images/filename.ext" /> or the images need to be embedded into the email.
The reason behind the image is not show in email because email client cannot get path you
specified.
For the solution you can do below
First replace the simple image URL to publically access url
[means if you copy the image URL in browser you can access the browser], make sure your image folder is out of authentication
As Jason said you have to put whole url, but if you put your image in App_Data they won't be accessible from outsite because this is a protected folder. You have another option to put images in mail, IMO this is a prefered way to put images in mail, e-mail client won't complain and ask for permission to display images. Here is a example how to do this
http://www.codeproject.com/KB/aspnet/EmbedImage.aspx
you can also use google picasa.
upload the images to picasa, set the visibility to public, get in the album and on the right
side you will have link to this photo link.
press this link and chose the size you want it to be displayed, mark the image only
check box and you will have a link ready to embed in to the mailer.
put the link in the src and that is it.

How to load an image from the server in a web application in C#

I actually have a C# winform application which load images from my computer C://images/... with the Image object and the function Fromfile.
Image.FromFile(Path);
but in my web application (ASP)
<asp:Image ID="viewPhoto" runat="server" Width="550px" Height="400px"/>
I use the attribute ImageURL.
viewPhoto.ImageURL = Path
But the problem is that it doesn't find the correct path because with this way. The path will be http://localhost:3656/C://images....
I would like to load an image directly from my server to have the correct path for both of my applications.(web ASP and winform)
Image.FromFile(/images/myimage.jpg)
This actually doesn't work because the program doesn't find any photo in this path.
First of all i think that images you are trying to show are not in web application folder / virtual directory. Move images folder to your web application folder and then use:
Page.ResolveClientUrl("images/test.jpg");
or for server side:
Server.MapPath("images/test.jpg");
If you dont want to move images to your web folder then your only choice is to write HttpHandler which will read images from C:\images folder and transmit it to the client. This will also require some specific permissions for your web app IIS user to access some folder outside the web app scope.
You can see the sample of HttpHandler here: Thumbnailer HTTP Handler
ASP.Net image simply renders the ImageURL to the client browser, this will then try to load that image from the location specified, this typically needs to be a resource available via your website.
Try moving your images inside your web root then you can access them like :
viewPhoto.ImageUrl = "~/images/yourimage.jpg";
As im sure you know your <asp:Image> element is actually creating an html <img> element that will instruct the browser to request the image from the browser.
Image.FromFile will give you an image object on the server side but it won't be much use for your clients browser.
As #HABJAN said, you need to map the path from your local file to a relative web URI and to do that you can use Server.MapPath. However your images will need to be inside your asp.net project folder which is asccessible by the web server in order for it to serve the file.
If you really want to share the paths in some sort of shared constant assembly I suggest you make your windows application work with relative paths and duplicate the images folder structure for both the windows app and the web app.
I found the quickest way to resolve this was to assign the image path as a css attribute. The only downside of this is that if you are looking to display a large amount of images in a repeater you would not want to use this, but if you have one or two images you can use this.
<style type="text/css">
#errorImage{
background: url(/path/path/images/image.jpg);
background-size: contain;
height: 100px;
width: 100px;
}
</style>
<div id="errorImage"><div>
Using the path you are using now (http://localhost:3656/C://images....), the application will look for a /C://images... folder in your app directory on the web server. I believe you should create an images directory in your application folder and server the images from there so that you could reference the images using a relative path.

Categories

Resources