Access folders in root directory - c#

Hi I am developing an asp.net web application. I have to access one of the image in images folder in root directory. I am using following code in my code behind file.
string imageDirectory = HttpContext.Current.Server.MapPath("~/images/");
string imageUrl = imageDirectory + "/img1.bmp";
This works fine in my local machine. My question is does this code work when I move my application to production ?

It should as long as you have an application root/virtual directory for your site.
Also, you can combine these two lines into:
string imageUrl = HttpContext.Current.Server.MapPath("~/images/img1.bmp");

If you're thinking of putting imageUrl into an <img> tag, then no, it won't work. Server.MapPath will return your file or directory as a local Windows file/directory name, so something like "C:\WebRoot\MyWebApplication". If you send this to the browser, obviously, the browser won't pick up the image.
What you can do is something like:
string imageUrl = ResolveClientUrl("~/images/myImage.gif");

Related

Where do my images go when i use FileUpload1.SaveAs(Server.MapPath())?

Originally I started having the images put into a database and using generic handler to view the images, it was ok for one part of my app but then I realized that storing images in the database wasn't going to work out to well for a part of my application because I will be dynamically creating an html table with hyperlinks and using images for hyperlinks.
So using generic handlers would see to be a huge mess when creating the html and navigation hyperlinks,
So what I have opted to do is now is put these images to a folder on the server, but right now I am using my laptop before I even get to the point of publishing the app on line.
So this is the code I am using...
string iconName = fpIcon.FileName;
string iconExtension = Path.GetExtension(iconName);
string iconMimeType = fpIcon.PostedFile.ContentType;
string[] matchExtension = { ".jpg", ".png", ".gif" };
string[] matchMimeType = { "image/jpeg", "image/png", "image/gif" };
if (fpIcon.HasFile)
{
if (matchExtension.Contains(iconExtension) && matchMimeType.Contains(iconMimeType))
{
fpIcon.SaveAs(Server.MapPath(#"~/Images/" + iconName));
Image1.ImageUrl = #"~/Images/" + iconName;
}
}
So my question is, and don't laugh to hard but, where are these images being stored? I can't find them anywhere on my laptop using the windows search. With all the images that I have in this ~/Images directory, I can change the image to any image I wanted by supplying the name, but I have no idea where the images are being held, and when I do deploy to a site then where are they going to be then?
Thanks
Server.MapPath gives your current website directory(IIS hosted or running from VS). So go to your website directory, find Images directory there and you'll see your images.

image control not showing image

What i Want to Do!
I want to show an image using image control. The source image is on file directory. The location of file is C:// directory while my project (Virtual Directory) is on D://. I want to set image source at page load.
What i have Done!
Put image control on my .aspx page.
On page load set imageurl to my suggested url
Following is the code I've written
Dim urls As List(Of String) = TryCast(Session("SliderUrls"), List(Of String))
Dim url As String = urls.Item(4)
Image1.ImageUrl = url
Note
url value is assigned properly. There no issue in url. On internet at some websites I've read that asp.net don't allow us to access resources outside virtual directory. So do you think that this may be the problem that i'm facing? and if so then how can i generate url for another virtual directory. Like i have a virtual directory on D://myproject and another virtual directory C://files. How can i generate url for Virtual directory on C://file while working in project which is in Virtual Directory D://myprojec.
The urls / paths have to be virtual and not physical.
Even though you've made the directory virtual, you appear to be trying to use the physical path instead.
Trying to use the physical path (C:/path) will not work.
Try using the base url of the other virtual directory and build your url from that.
For instance if the virtual directory is http://localhost/media
use that as the base url and attach the resources from there http://localhost/media/image.jpg
Dim baseUrl as String = "http://localhost/files"
Image1.ImageUrl = baseUrl + "/" + System.IO.Path.GetFileName(urls.Item(4))
I'm assuming the urls contains only the filenames (image.jpg for instance)
you can also store the baseUrl in web.config appSettings

Saving images relative to content directory where my site is running in IIS

I want to save images uploaded from a asp mvc page to the content/img/ folder which is relative to where my site is running in IIS. But I don't want to hard code the location on my file system to a Absolute path as this could change easily. This is the code I'm using to save them.
public static string SaveCompanyLogoImage(HttpPostedFileBase file)
{
var newFileName = GetNewCompanyLogoFileName(file.FileName);
file.FileName = newFileName;
file.SaveAs(//relative-location-here);
return Path.GetFileNameWithoutExtension(last);
}
Hope this explains my issue!
Thanks for any assistance
Use Server.MapPath("~/MyImagesFolder")
http://msdn.microsoft.com/es-es/library/system.web.httpserverutility.mappath.aspx

Accessing a network folder with ASP.NET

I need to access a network folder in
\\p3clfs\xyz\test.tiff
Then:
String image = #"\\p3clfs\xyz\test.tiff";
MyClass.OpenUrl(image)
Error: c:\inetpub\wwwroot\p3clfs\xyz\ does not exist!
How can I open the URL without "c:\inetpub\wwwroot\"?
If you need to open a network path (so, something based on the file system,) as a URL, your string needs to be something like this:
String image = #"file://p3clfs/xyz/test.tiff";
Actually the solution was to create a virtual directory in IIS.

Load local HTML file in a C# WebBrowser

In my app I have a WebBrowser element.
I would like to load a local file in it.
I have some questions:
Where to place the HTML file (so that it will also be installed if a user executes the setup)
how to reference the file? (e.g. my guess is the user's installation folder would not always be the same)
EDIT
I've added the HTML file to my project.
And I have set it up so that it gets copied to output folder.
When I check it it is present when run: \bin\Debug\Documentation\index.html
However when I do the following I get a 'Page cannot be displayed' error in the webbrowser element.
I use the following code to try to display the HTML file in the Webbrowser.
webBrowser1.Navigate(#".\Documentation\index.html");
Do a right click->properties on the file in Visual Studio.
Set the Copy to Output Directory to Copy always.
Then you will be able to reference your files by using a path such as #".\my_html.html"
Copy to Output Directory will put the file in the same folder as your binary dlls when the project is built. This works with any content file, even if its in a sub folder.
If you use a sub folder, that too will be copied in to the bin folder so your path would then be #".\my_subfolder\my_html.html"
In order to create a URI you can use locally (instead of served via the web), you'll need to use the file protocol, using the base directory of your binary - note: this will only work if you set the Copy to Ouptut Directory as above or the path will not be correct.
This is what you need:
string curDir = Directory.GetCurrentDirectory();
this.webBrowser1.Url = new Uri(String.Format("file:///{0}/my_html.html", curDir));
You'll have to change the variables and names of course.
quite late but it's the first hit i found from google
Instead of using the current directory or getting the assembly, just use the Application.ExecutablePath property:
//using System.IO;
string applicationDirectory = Path.GetDirectoryName(Application.ExecutablePath);
string myFile = Path.Combine(applicationDirectory, "Sample.html");
webMain.Url = new Uri("file:///" + myFile);
Note that the file:/// scheme does not work on the compact framework, at least it doesn't with 5.0.
You will need to use the following:
string appDir = Path.GetDirectoryName(
Assembly.GetExecutingAssembly().GetName().CodeBase);
webBrowser1.Url = new Uri(Path.Combine(appDir, #"Documentation\index.html"));
Place it in the Applications setup folder or in a separte folder beneath
Reference it relative to the current directory when your app runs.
Somewhere, nearby the assembly you're going to run.
Use reflection to get path to your executing assembly, then do some magic to locate your HTML file.
Like this:
var myAssembly = System.Reflection.Assembly.GetEntryAssembly();
var myAssemblyLocation = System.IO.Path.GetDirectoryName(a.Location);
var myHtmlPath = Path.Combine(myAssemblyLocation, "my.html");
What worked for me was
<WebBrowser Source="pack://siteoforigin:,,,/StartPage.html" />
from here. I copied StartPage.html to the same output directory as the xaml-file and it loaded it from that relative path.
Windows 10 uwp application.
Try this:
webview.Navigate(new Uri("ms-appx-web:///index.html"));
Update on #ghostJago answer above
for me it worked as the following lines in VS2017
string curDir = Directory.GetCurrentDirectory();
this.webBrowser1.Navigate(new Uri(String.Format("file:///{0}/my_html.html", curDir)));
I have been trying different answers from here, but managed to derive something working, here it is:
1- Added the page in a folder i created at project level named WebPagesHelper
2- To have the page printed by webBrowser Control,
string curDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
var uri = new Uri(curDirectory);
string myFile = Path.Combine(uri.AbsolutePath, #"WebPagesHelper\index.html");
Uri new_uri = new Uri(myFile);
i had to get the assembly path, create a first uri to get an absolute path without the 'file://' attached, next i combined this absolute path with a relative path to the page in its folder, then made another URI from the result.
Then pass this to webBrowser URL property webBrowser.URL = new_uri;

Categories