Can't read images from C:\ in ASP.NET - asp.net

I realize this is an edge case, but it seems that asp.net doesn't allow reading images from the root drive. To test this I wrote the following very simple application:
On the page I have:
<asp:Image ID="test" runat="server" />
In the code behind I have:
test.ImageUrl = #"C:\test\image.jpg";
test.AlternateText = File.ReadAllText(#"C:\test\text.txt");
Both files exist as you'd expect and have the correct permissions. When viewing the source of the page we get an img tag that looks like the following:
<img id="test" src="C:\test\image.jpg" alt="some text to read" style="border-width:0px;" />
When I run this in VS no image is displayed. If I copy the source, paste it into an html file and load it into a browser the image is displayed. Can anybody explain why this is?

The src of an img tag should rarely point to a file on your local disk. It should be an URI. Perhaps you'll have more success using the file:///C|/test/image.jpg syntax.

The browser is preventing the images from being displayed. I created a static HTML page containing an tag whose "src" pointed to an image on my local file system. If I open the HTML page locally, everything works as expected. However, when I hosted the static HTML page in a web server, it stopped working.
I ran ProcMon while loading the HTML page from the remote server, and the local file was accessed (some columns truncated for display purposes):
iexplore.exe 6376 QueryDirectory C:\test.jpg SUCCESS test.jpg
iexplore.exe 6376 QueryOpen C:\test.jpg SUCCESS
iexplore.exe 6376 QueryDirectory C:\test.jpg SUCCESS
iexplore.exe 6376 CreateFile C:\test.jpg SUCCESS
iexplore.exe 6376 ReadFile C:\test.jpg SUCCESS
iexplore.exe 6376 CloseFile C:\test.jpg SUCCESS
However, IE did not display the image. I repeated the test with Firefox. When accessing locally, the ProcMon results were the same. When accessing remotely, ProcMon didn't produce any output.

It has to do with the way you're assigning the img src. By providing C:\test\image.jpg as the sourc you're instructing the BROWSER to get the image from the user's local drive rather than from the server's location.
All src directories/files should be specified using relative paths for your website. For example: If your homepage was located on your server at c:\www\homepage.aspx and you also had an images sub-directory located within the www directory then you'd want to specify your img src to be something like this: <img src="/images/image.jpg" alt="Image" />

I know that you said the permissions were correct, but double check and make sure the aspnet\aspnet_wp account has permissions to the file location. When you run an asp.net application it runs under this account. When you load an HTML file in your browser, it runs as you.

This is because you're feeding a "URL" into the src tag, and the browser treats it as such. When you open a file such as C:\test.html it is simply rendered by the web broswer and uses the local source as the starting URI, but when you load from another source, such as http://localhost/test.html then the file needs to be accessible through http, and http://C:\test\image.jpg is not a valid URL.

Seems to work properly if you use a url. Not sure what the difference there would be though.

This is a browser security "feature". The goal is to prevent malicious websites from accessing your local files, and causing them to either be executed or uploaded to the website without your knowledge.
In order to get past it I believe you will have to change your security zone to "local intranet", then drop the security down to pretty much allow everything.
Obviously this is only viable for machines you directly control.

The html code is invalid. It should not render in any browser, VS, firefox, IE or any other. In fact it does not in firefox.

If You display images from C: drive or any local drive in a server side web application development. It will create conflict-ion from client drive if they are same file and folder name in client computer. That's why you Can't read images from C:\ in ASP.NET.
To read the images from local server drive first you will to create virtual directory for your local drive(C:\Gallery) then you can display images from folder.
example:
<
asp:Image Id="Image1" ImageUrl='<%# Eval("Filename", "http,://localhost/ImageGallery/{0}") %>' Width="150px" Height="150px" runat="server" />
please remove from http, like http
Shankar Chaurasia

Related

CSS changes automatically applied on local IIS pages

I'm hosting an ASP.NET website on Local IIS (not IIS Express), and as soon as I save a change to a .css file in Visual Studio, the change immediately appears in browser windows that use that file (or after mousing over the window in Chrome), without clearing caches and refreshing.
Why do the changes appear immediately?
Opening the .css file itself (not a page using the file) in the browser shows a more expected result: saving the file in Visual Studio does not change what I see in the browser until I refresh the .css file.
As it turns out, I had Browser Link enabled in Visual Studio, and with it, CSS Auto-Sync. This opens up a port on the local machine and uses SignalR to communicate with the browser window about 400 times per second, including any CSS changes needed.
For more information, see these topics:
.net localhost website consistently making get arterySignalR/poll?transport=longPolling&connectionToken= calls
How can I disable __vwd/js/artery in VS.NET 2013?
This probably happens due to caching. when you open the css itself, it retrieves a new copy from the server, but when you open a page that uses the css file, the css file is being cached as the page's resource and the browser just shows the cached resources until you force it to reload them.
a trick i learned to fix the issue, is to link the css file to the aspx page and include a random query string to the linking, that way it tricks the browser to think that its a new resource and reload it from the server anyway.
like this:
<link href="../stylesheets/MyCSS.css?<%=DateTime.Now%>"
rel="stylesheet" type="text/css" />
we use the aspx preprocessor directive <%=DateTime.Now%> to append the current time as a query string, to ensure the link is always different.
Dont forget the question mark between the css filename and the preprocessor directive

ASP.NET WebForms: Why do relative paths within user controls work locally, but not when deployed?

Rewriting Question
I cannot, for the life of me, get relative image paths work consistently inside usercontrols between using VS's dev server and publishing to a remote IIS server. I do not want to make my image a server tag by adding runat="server". I want it to be strictly client and I do not want to use any hocus pocus code hacks. I want to resolve the disconnect, so here is my problem:
My images are stored in ~/images.
My usercontrols are stored in ~/usercontrols/anothersubfolder.
When I do this...
<img id="myimage" src="../../Images/help.png" alt="" />
...the image loads when using the VS dev server (localhost), but not when I deploy to a remote IIS virtual directory. However, when I do this...
<img id="myimage" src="<%=Request.ApplicationPath %>/Images/help.png" alt="" />
...it works when I publish remotely, but not using the dev server!
I need a solution that works for both the VS dev server AND when I publish remotely. As I said, I do not want to add runat="server" to my image tag or use any code hacks. There is a disconnect here and I want to know how to go about resolving that.
Try it like this:
<img src="~/Images/indicator.gif" border="0" alt="" runat="server" />
Avoiding problems with relative and absolute URLs in ASP.NET
Control.ResolveUrl versus Control.ResolveClientUrl versus VirtualPathUtility.ToAbsolute
ResolveUrl vs. ResolveClientUrl
It seems like you've found your own answer: The location of the ascx control doesn't change relative paths. The aspx page loading the ascx control ultimately determines the paths (this will extend to your CSS and JS files, if you're using any).
I "solved" this problem by never using relative paths. My src attributes always look like <img src="/<appfolder>/Images/<filename>.<extension> />.
It's worth noting that I change my website properties and select Use Local IIS Web Server under the Web tab of the properties page. Since I don't use the Visual Studio Development Server, your mileage may vary depending on your project's configuration.
As others already answered, a user control ".ascx" is being loaded by the page that contain it and btw it is the same thing with master pages - they are being loaded by the page.
So any relative path you describe in your script is relative to the location of the page.
So putting <img id="myimage" src="~/Images/help.png" alt="" runat="server" /> or simply use the server side control for image, will solve your problem technically..
But from what I understand your problem is less technical as you probably knew that..
So left to assume is that your problem is more conceptual regarding the morphing of a simple html control into what you describe as 'code-hack'.
You must acquire yourself with the "standards" regarding working with asp.net,
and to do that you first need to understand that asp.net is a system that aims to generate a page on server side and afterwards deliver this newly created dynamically generated page to the client side - this is the main benefit of working with asp.net and is the highest principal of it!!
This is why your page in asp.net is not .html - it is .aspx even though the script generated later will only be html.
So if you agree with my previous paragraph you also understand that any .aspx page you create is being processed by your server before delivery and the script tags you put in it are just a set of instruction regarding of how to process it dynamically. adding runat="server" only means that the attributes and content of a script tag requires server side special attention during the generation process of the resulting html page.
The aspx page you have in VS and the aspx page generated for your browser are not the same entity.
runat="server" never appears to the script being delivered to your client side.
Writing script in asp.net is not writing html - it is writing meta instructions that aim to influence what the generated html would contain.
I hope that by now I convinced you to use the provided best technical solution as a normal and natural standard in asp.net with no grief or regrets.
<img id="myimage" src='<%= ResolveUrl("~/src/....") %>'/>
when you run it locally you are using your folder you created and you are working in, after publish your control maybe ended up in a controls folder created by VS and your original filestructure with 2 folders is gone. Check the filesystem after publish and reorder your work-folders.
Relative paths in user controls are relative to the page they appear on, not the location of the user control. This is basically what you are stating in your update, I believe.
You should post your update as answer and accept it, or accept my answer, whichever is your preference :)
Just as another possibility, ASP Parent paths are disabled on IIS by default, could this be the cause of your issue?
This article talks about how to resolve the issue if it is this. Basically, you can resolve it in a couple of ways, but the easiest is to go into the ASP settings on the IIS site and re-enable parent paths.

referring the image files from the Trdion server in Dreamweaver TBB in SDL Trdion 2011 Sp1

I am trying to refer the Images from the Web server of the Tridion.
<img src="file://///ServerAddress.com/applications/Images/Styles/temporary.jpeg" alt="alt text"/>
I have added this code my TBB.
The image is not getting displayed.
Added the same code in local html page in my machine, I am able to retrieve the that Image.
Edit:
Image is not getting displayed while previewing in Tridion CME.
Can any one suggest how to configure my image source path in TBB for accessing the Image.
Thanks in Advance.
You are using a file:// URL, which means that you are referring to a local file on the machine that opens the HTML. So when you open the HTML on the machine where you originally created it (and placed temporary.jpg), it will find the file locally and display it.
When you open the same HTML from any other machine, it will fail to find the file on that machine and thus won't display the image. The fact that you use ServerAddress.com makes no difference for that: file URLs will only resolve to file on the local machine.
If you want to ensure your images work properly for the rest of the internet too, use a more common protocol such as http://.

html code doesn't run in the browser, and only display the code

in order to find the server spec. i've created a file in the root dir in my website called spec.htm and entered this content as i was offered by another user:
<html>
<head>
<title></title>
</head>
<body>
#ServerInfo.GetHtml()
</body>
</html>
but i only get a copy of the code in my browser and it doesn't run it,
what the problem might be?
EDIT: i think that the problem is that i'm not using IIS.
is there a way to do so without using IIS?
thanks
Please see the following article
http://www.asp.net/webmatrix/tutorials/14-introduction-to-debugging
The ServerInfo helper is a diagnostic tool that gives you an overview of information about the web server environment that hosts your page. It also shows you HTTP request information that is sent when a browser requests the page. The ServerInfo helper displays the current user identity, the type of browser that made the request, and so on. This kind of information can help you troubleshoot common issues.
Create a new web page named ServerInfo.cshtml.
At the end of the page, just before the closing tag, add the following highlighted code.
#ServerInfo.GetHtml()
Note, it appears as though this is designed to run in IIS only and not on Linux / Apache servers.
Note, this is a RAZOR syntax so your system needs to be able to run Razor by installing the WebMatrix
#ServerInfo.GetHtml() is a Razor view engine syntax. Try saving your file as .cshtml or .vbhtml
The problem is that your webserver is not set up to serve HTML files through the ASP.NET interpreter. Change the extension to .aspx (i.e., use the same code, but call it spec.aspx).
Are you setting the Content-Type header correctly. If not set to text/html or similar, the browser or framework may set the content-type to text/plain which will not render the html at all.
You can check this in Firebug in the Net tab, expanding the response that is associated with the page you are serving, and looking in the Headers tab. If the Content-Type header is anything besides text/html or text/xhtml, then you need to find a way to make your web server set that header properly
let me guess its just showing up "#ServerInfo.GetHtml()" on a webpage. This does nothing if you put it simply in a body tag of a html page. If you are running IIS make sure you are saving as .aspx and not .html
See "yourhtmlsource.com/myfirstsite/myfirstpage.html"
I hope I am understanding the question and this helps. I found it on the web page given above.
When you double-click a file on your computer’s desktop, the computer knows what program to open the file in by checking the file’s “extension”. A txt file will open in a text editor.
You need to give your document a file extension of ”.html”, which will tell it to open the file in your web browser, such as Internet Explorer, Firefox or Safari.
Right now you should be editing your HTML page in a text editor, which normally saves files with the extension “.txt”. We want to make it save in “.html”. In your text editor click File → Save As…. If you use Microsoft Windows, there will be a box labelled “save as type”; change it to “all files .”. This means that you can save the data (in this case, some text) into any format. Now type in the name index.html for your file and click save. Ex: file.txt becomes file.html.

ASP.NET rdlc with external images not displaying images in PDF

I'm using the Microsoft ReportViewer that comes with ASP.NET and have a report parameter that should be setting the value (path) of an image in my report. I'm providing the path as a complete URL right now, starting with http:// but have also tried this as an app relative path, site rooted path, etc. and for some reason the image is always showing as the red X when it exports to PDF. I'm just creating an instance of a control in code, setting the properties and exporting directly to the response stream so it acts a download.
I'm just not sure what the problem could be with the image not showing up, so if anyone has any ideas please let me know.
UPDATE 1
I've determined that I can embed the image with a URL if it is on my public web server but when I'm running in localhost the image won't embed. I have confirmed for localhost that if I paste the same URL into my browser the image will open fine. As far as I know, I don't have a proxy. So I can work around my issue, but I still don't understand what the problem is with localhost.
UPDATE 2
Forgot to mention that when the URL to the image is opened from a browser it works fine.
It is not possible for a PDF to contain a reference an external image (at least from my understanding). In order for an image to appear in the PDF, it must be embedded into the document. Therefore, to use an external image, your app must retrieve the image and store it in the document. The report viewer will try to do this for you.
Two possible answers:
First, in order for your app to package the image into the PDF, it must be able to retrieve the image from the URL you are specifying. If that URL is behind a proxy (from the perspective of your app server) and/or requires credentials to access, this will present a challenge with the default configuration of the report viewer.
If a proxy server is the issue, please see the settings to your web.config you can add below. You may also need to supply network credentials, so your app can authenticate to the proxy. There are lots of ways to solve this, but one of the easiest is to run your application as a service account on your domain that has rights to traverse your proxy. You can test this by running the site as you temporarily (should be temporary because this is a horrible security practice).
The image you are using could require credentials to access (try pulling up the image in Firefox with empty cookies and verifying whether credentials were required to access it). If it requires Windows authentication, the same solution to proxy security may apply to authentication required on the remote image. If it requires some other form of authentication, you may be better off downloading and embedding the image into your project.
It is also possible to download the image using other means in your code and convert it to a byte array for inclusion in the report. There are lots of examples of this on the web, including a Stack Overflow here.
Second, take a look at the following page:
http://msdn.microsoft.com/en-us/library/ms251715%28VS.80%29.aspx
Using external images in a
ReportViewer report is not enabled by
default. To use an external image, you
must set the EnableExternalImages
property in your code. Depending on
your network configuration, you might
also need to bypass proxy settings to
allow the external image to appear.
You can add the following settings to
the Web.config file to bypass the
local proxy. When modifying your
Web.config file, be sure to specify
the name of the proxy server that is
used in your network:
<system.net>
<defaultProxy>
<proxy usesystemdefault = "false" bypassonlocal = "true" proxyaddress = "http://< proxyservername >:80/" />
<defaultProxy>
</system.net>
Hope one or both of these helps.
Jerry
When passing external image filenames to ReportViewer parameters, pass the format like this: file://C:\app\images\pic.jpg. Anything else usually doesn't work well when deployed.
Okay, so this was our solution. The web server did not recognize its own qualified DNS name as a URL, so we had to edit the Hosts file in the C:\Windows\System32\drivers\etc folder and add the host name as localhost. The line we added to the file was:
ourserver.ourdomain.com 127.0.0.1
I don't think Adobe Reader (or maybe the PDF specification itself?) allows external content to be loaded for security purposes. I vaguely remember having a similar issue that had nothing to do with reporting services (I was dynamically generating PDFs and using variable logos and had to embed them).
Did you try a regular file path (c:/temp/somefile.bmp)? Reporting services local report reads the file from the disk and embeds it in the pdf file produced. Make sure that the identity of the app pool in IIS has read permission on the image file.
We are doing it and our images are placed in an img folder under the web site, along withe the rest of the web sites images. We avoid hard coding the path by using Server.MapPath(relative path).
Hope this helps
I fixed my problem with this:
//For local relative paths
string imgUrl = new Uri(HttpContext.Current.Server.MapPath("~/images/mylocalimage.jpg")).AbsoluteUri;
// OR
// For complete URLs
{
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; // This allows reportViewer to download image from url
string imgUrl = /* Your image URL ("http://") */;
}
//Then pass imgUrl parameter as external source of your image.
Can the report viewer get an image from a relative url? I've never used it, so best to check that assumption.
Have you tried using the Html.Content() helper to set the URL? Whenever I have issues with my urls its because I didn't use this to generate the correct url for the view.

Resources