I want to take files from a known location on disk and have ASP redirect to them from code-behind, allowing the browser/app/device to control how the content is displayed.
I tried using:
Server.Redirect(pathToFile);
But got the following exception: Invalid path for child request 'C:\ContentFolder\testImage.png'. A virtual path is expected.
How can I allow my site to redirect users to these files? I am storing the base directory in the web config, and the file names are stored in a database.
If the files reside outside of your website directory you can't redirect to them. Think about the security implications if that was possible. You have a few options
Move the files inside the website directory (or a sub-directory of it). Then you can redirect your users to them using a virtual path e.g. Server.Redirect("~/files/somefile.zip").
Set up a virtual directory in IIS that maps to the physical location of the files on disk. Then you can redirect to them using the virtual path. You can do this through the GUI or config file.
Create a HttpHandler that loads the file from disk and returns them in the response. You can use a querystring to identify the file to load e.g. /filehandler.ashx?filename=somefile.zip. A quick google revealed this example.
Related
I have a "client-styles.css" in my wwwroot / client_folder folder. When I run my project in localhost and give that css file path in the browser its showing the entire css file in the web browser.
( localhost:5000/client_folder/client-styles.css ).
In the same way I have a folder also contains json files which contains sensitive information and those are also showing if we give the path to those Json files ( localhost:5000/client_folder/client-secrets.json ) in the browser. Is there any way restricting some files to view from web browser.
Thanks
Based on your last comment above:
It's perfectly acceptable to store css and javascript files in wwwroot. However, do not store anything secret there. Storing secrets like connectionstrings are best in EnvironmentVariables.
I have an ASP .Net site which requires users to log in i.e. http://www.example.com.
They login and upload images which are stored under the images folder, under the root of the site i.e.
MySite
App_Code
App_Data
Images
Employee Images
Employee1.jpg
Employee2.jpg
Employee3.jpg
Country Logos
Country1.jpg
Country2.jpg
Country3.jpg
....
I now have a second project completely separate from the above. I need to display the images from the above location but if i use http://www.example.com/images/ i am forced to log in.
How could i access the images without logging in?
You need to write controller function in order to access files in the protected directory. Then control access to that controller.
I have two asp.net MVC websites. One the front end(mysite.com) and one the backend(admin.mysite.com).
They both use the same database and everything is working fine. But, I am facing the issue in upload. I want to upload images in front end content folder from the admin website. How can that be achieved?
Using Server.MapPath("~/Content/Images/Products/product") locates the folder in admin website.
If you have access to the iis server, you could set up a virtual directory that is pointed at the desired folder on your front end site.
All that would be required from the front end is that you point the upload path to the virtual directory.
Server.MapPath("~/Files") returns an absolute path based on a folder relative to your application. The leading ~/ tells ASP.Net to look at the root of your application.
To use a folder outside of the application you can use the full path:
#"E:\Project\Folders\Content\Images\Products\product"
You can also get the full path directory:
string currentDirectory = Directory.GetCurrentDirectory();
I have a zip file stored in app_data folder .I want to give the path ofthat file as URL for a hyperlink.How to assign it
Server.Mappath(~/App_Data/Test.zip) will give the physical location as D:/Projects/Mnv/App_Data/Test.zip , but i want to give it as URL like http://..../test.zip, so user can download
You should not use App_Data available to the users.
This is a special folder used to store internal data only (XML, sdf, etc..).
By default, this folder is not even available via Http.
Yo can create an Action in your Asp.Net MVC and use it to retun some file from this folder. A direct download, however, it is not recommended.
I have a simple HttpHandler which gets the image file from the specified path in the callig URL. For instance, when this URL: http://www.abc.com/images/imageview.ashx?fileName=ok.jpg is called, it will write the file from the path: d:\images\ok.jpg using response.writefile.
Everything works fine, but the folder named images in the web site root path (d:\inetpub\wwwroot\images) is locked and after the first call, I could not rename or delete the folder. I don't use the folder (in the web site root) any where!
I've checked all files for the second folder and they are not in used, only the folder is in used.
When I changed the calling URL to http://www.abc.com/imageview.ashx?fileName=images/ok.jpg the problem resolved.
It seems that IIS7 locks the virtual folder for ashx URL.
Any idea or experience?
It's because the image is resolving most likely relative to the folder the handler is in. Either put an absolute path, like /images/ok.jpg, or on the server call ~/images/ok.jpg to resolve it properly. See more about ~ here, http://weblogs.asp.net/fmarguerie/archive/2004/05/05/avoiding-problems-with-relative-and-absolute-urls-in-asp-net.aspx