I need to display an Image in web page. But the Image doesn't exist in the Web directory. If the image is under web directory I know that just "../Images/TN/my.jpg" will work. But the image is available in "D:\Images\TN\my.jpg" and My web site is deployed in "C:\apps\mywebsite".
How do I convert the "D:\Images\TN\my.jpg" path to a relative path so that the Image will be visible in web page?
You cant show images if they are not in a virtual directory.
Try holding then in your application folder itself or if its not feasible then make your Folder holding the image as a virtual directory in the IIS and mount it in your application folder.
Here's what you need to do (way i prefer),
Open IIS manager.
In Connections under Sites Select your Website and RightClick on it.
Select `Add Virtual Directory`... from the Context menu.
In The dialogue box that opens Enter the Alias name of your choice like **`"GlobalImages"`**
Browse the physical path of the Folder you want to and Select it and Click OK.
You will have it available for relative url in that website. like
www.example.com/GlobalImages
When you set that in IIS, it is automatically added to your Solution's Project.
Here is the MSDN link if you want..
http://msdn.microsoft.com/en-us/library/ms751432.aspx
Easiest thing to do is probably set up a handler and use the generic image name. While local, point to the D:. While on the server, point to the web path. (At least so long as you're not keeping them within the virtual directory that is).
TO fetch this point, we always write the absolute directory for images or other resource.But if the resource in the web server, i use the relative directory.
Related
I have the following structure:
AppName
Content
img
file.png
I can navigate directly to mysite.com/Content/img/file.png without any issues.
However when I add test.txt to img and set to Copy Always and type Content (same as file.png), I get No webpage was found for the web address when visiting the link, when I want to be able to enable folks to download the publicly accessible file (no auth needed), just like the image. Why isn't this working?
I can see test.txt in the bin folder of the app. Does something else need to be configured to enable direct file downloads?
Make sure your IIS site settings or web.config have mime type associated with .txt extension.
https://www.iis.net/configreference/system.webserver/staticcontent/mimemap
I am using this code for creating pdf viewer in my application
https://amitpatriwala.wordpress.com/2009/08/28/pdf-viewer-in-asp-net/
it works fine when I give it a path for a file inside my application folders, ex: displaypdf1.FilePath= #"~/MyFolder/" + Hello.pdf;
but now I want to give this displaypdf1.FilePath an absolute path to read the pdf file which is not in my application folders, I tried but it didn't work!
A web page cannot access items that are outside of the website. If you want the web page to reference files located in D:\PDFs, for example, you need to create a virtual directory in your website that points to "D:\PDFs". Then the web pages can access them by ~/PDFs/Hello.pdf.
You'll also need to ensure that the website has appropriate permissions to access the directory.
There is a folder for file uploads on my site http://mySite/Uploads/
for each user I want to create a new folder and save files into it. It works for site visitors/ But i also have an admin site. Employees should be able to upload files with it at the same directory.
Directory.CreateDirectory returns an error, that it can't handle uri addresses.
I'm passing "http://mySite/Uploads/UserId". am i wrong? should i use another mechanisms?
am i wrong?
Yes, you should pass a physical folder to the Directory.CreateDirectory method and not an url. For example:
Directory.CreateDirectory(Server.MapPath("~/uploads/UserId"));
The Server.MapPath method should return a folder like this: c:\inetpub\wwwroot\mysite\uploads\userid.
You must use paths relative to the system root. You can do that with the MapPath method.
var uploadsRoot = Server.MapPath("~Uploads");
var userUploadFolder = Path.Combine(UploadsRoot, userId.ToString());
Directory.CreateDirecty(userUploadFolder);
If anyone interested. We made virtual folder "uploads" in IIS in both sites. Right click on your site in iis => add virtual folder or somehow, my iis isn't in english. Do it for both sites. and it's full path points at the same folder, so i can use Server.MapPath("~Uploads"); and in admin site i get http://adminSite/Uploads/, in main site it's http://mySite/Uploads/. if i upload files by one address, i allways can get access to them by another.
I am trying to upload image files to the server and it gives me an error
"System.UnauthorizedAccessException: Access to the path 'D:\Hosting\234344\html\Testingfiles\upload\813.jpg' is denied.at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)"
in the HttpHandler I have :
HttpPostedFile file = context.Request.Files["Filedata"];
string usr_id = context.Request.Form["usr_id"];// gets the JSON data from the request
string strPath = context.Server.MapPath(("/Testingfiles/upload/") + file.FileName);
string ext = Path.GetExtension(strPath);
if (ext.Equals(".jpg") || ext.Equals(".jpeg") || ext.Equals(".png"))
{
file.SaveAs(strPath);
context.Response.Write("Image uploaded successfully");
}
what am i doing wrong here?
The error message says it all. You don't have write access to that folder.
You will need to ask your hosting provider to assign write rights to that folder for the ASP .NET identity.
Also, consider if you can use a folder below ~/App_Data. This is by convention the place to store files that needs write access in ASP .NET, so many hosting providers will allow writes to this folder by default (but you would need to check yourself for your specific host to be sure).
You should try writing to ~/App_Data/ to see if that works. If it does then its just because you haven't given asp.net write permission to the /TestingFiles/Uploads/ folder.
If your control panel has Plesk on it then you can sort this out yourself by going to the FileManager and clicking the permissions button. If you look at your App_Data file permissions for reference, the actual username that you need to add will vary depending on your domain name with plesk.
Other hosting control panels may allow you to do it in different ways.
If you can't find it then you should ask your host how you set up file permissions or look in their knowledge base.
If your control panel has Plesk on it then you can sort this out yourself by going to the FileManager and clicking the permissions button.
I fixed this error by allowing IIS users full access to upload folder. No need to use App_Data folder
For Plesk 12.0 only:
No need to use App Data folder. You just have to give full control to your Application pool group IWPG(username). It will surely work. I searched for many hours and this solution worked for me .
Hope It works for others too.
How do I reference a file outside my web site's root directory?
For example my website is located at C:\dev\TestSite
I am using ASP.NET with XSP. The webapp will be deployed on Apache using mod_mono.
I have images in C:\images and I would like to do this:
<img src="C:\images\logo.gif"/>
Your img tag's src value is going to be sent to the client. You need to specify those paths relative to your document root. Your best bet is to set up a virtual folder (in IIS, alias is the apache equivalent) to point to the c:\images path and then change the mentioned tag src path as follows
<img src="/images/logo.gif" />
To do this in apache, you need an alias in your httpd.conf. The line looks like this
Alias /images c:/images
Here's the docs http://httpd.apache.org/docs/2.0/mod/mod_alias.html#alias
While it might be possible to do this through some hacky method, it's not a good idea. Allowing the IIS account to access files/folders on the greater file system would be a big potential security hole.
The best way to accomplish this is to use IIS Virtual Directories. Put your content in folders dedicated to supporting the site, DO NOT make your entire C: drive a virtual directory.
I'm going to assume that C:\images\logo.gif is the path on the server, and not the path on the client.
The src attribute is interpreted by the html client (i.e. Internet Explorer). The client can't see anything outside of your web directory. In fact, the client can only see things inside your web directory if you've provided permission for them to do so. Thus, this isn't an ASP.NET issue, but an issue of how web clients have access to web servers... which is designed this way for security.
In order for your application to use these images, you've got a couple of options that immediately spring to mind - neither of which is ideal:
The ASP.NET code (in the codebehind) is going to need to go and grab the file, and serve it out in the html stream that is being served to the client, which is more a complex task than I suspect you are willing to embark on.
The ASP.NET code (using System.IO) is going to need to go and grab the file from it's home location in C:\images\logo.gif and copy it to a location that is accessible to the client - you could create a temporary directory, copy your image to it, serve it out, delete it, delete the directory.
Both of these are certainly hacks that should be avoided if possible, but if you're adamant that this is what you want to do, this will allow you to do it via your ASP.NET app.
The most ideal solution is to add C:\Images as a virtual directory to your document root, i.e. /ImageCentral - this way you can have images that are central to multiple websites stored in this directory, it can then be referenced by clients for any of the websites just by adding virtual directories to each of them pointing at the central images folder. As DaveSwersky points out, don't make any directories containing sensitive information virtual directories, the minute you add a virtual directory to an externally visible website, you're giving people free reign to any of the information in it.
Good luck
You can't do this from within the HTML code of your page. The HTML page can only reference web accessible content (in regards to images, CSS, javascript, etc). You could create a virtual directory that points to your images folder so that it becomes web accessible.
EDIT:
The apache way inside of your conf file.
Alias /images "C:/Images"
And a little walkthrough from some dude.