I have a webproject where I want to read the Content/Images/Funny/ for all my funny images :-) I would want to generate a list in my controller of image urls and push it to the view.
How do I access it using relative path from project root? Dont want to use Server.MapPath since it will reveal my whole path on the server.
I started a new mvc3 project and Its Content folder I want to map up. Any hints?
in a controller's action, you can use
var path = new UrlHelper(Request.RequestContext).Content("~/Content/Images/Funny/");
Related
I have some problems with loading from path a model.
axis = Content.Load<Model>("XYZ");
I try to change it to :
axis = Content.Load<Model>("C:\\XYZ");
But it gives me this error :
Error loading "C:\XYZ". Cannot open file.
I also googled that thing, but I found nothing, looked at this question : How Do I Load A XNA Model From A File Path Instead Of From The Content (eg. Model.FromStream but it doesnt exist) , but it didn't help me.
In the properties of your content project, you specify a root folder in your project. When content is built, it is placed in your bin\content directory matching the hierarchy of your content project.
Content is local to your content project, not absolute on disk. Loading an unprocessed model from an absolute path will not get your a model inside of XNA like you expect.
I want to access to the file on my web project under content folder, and I want to use relative path. Can someone confirm me that I'm using right approach
string file = System.Web.Hosting.HostingEnvironment.MapPath("~/Content/myfile.txt");
yes its 100% right.
When you execute the code outside the context of a http request then HttpContext.Current is null and your code crashes. HostingEnvironment.MapPath always works
In this scenario i have used this approach to get multiple files and it worked fine ,so i recommend this .
try it once
String[] files = Directory.GetFiles(#HostingEnvironment.ApplicationPhysicalPath + "\\Videos\\");
I have a application in which I have to create folders dynamically on the server.The functionality is this that user click on the folder icon for creating the folder,after that give name to the folder and upload file in it.It is same like we do on "dropbox.com".
So,anybody suggest if there is any kind of example on the internet or any kind of API that is helpful for me. I have search a lot for this but couldn't succeed.
Thanks in advance
from your controller:
var path = Server.MapPath("~/Directories/YourDirectory");
Directory.CreateDirectory(path);
Suppose I have an Image located at “C:\A\mah” Or “E:\A\mah” my question is can I check the URL Path of such an image? If yes then how? Which will lots help me to put right URL path in my Asp.Net Web Application.
If those images are not within your project folder, you may need to create a virtual folder (say myImages) and then access images.
#Url.Content("~/myImages/imgName.ext")
Otherwise you could use
#Url.Content("~/path_relative_to_main_folder/imgName.ext")
Never use exact locations. It'll give you a headache. Instead, put a folder in your site's directory somewhere and then reference it with relative paths. For example:
<img src="#Url.Content("~/Images/ABC.png")" />
This means it is looking for the Images folder in your project's root directory.
Trying to display an image in the webpage tried like below.
Image1.ImageUrl = "C:\\Users\\naresh\\documents\\visual studio 2010\\Projects\\Vacancy\\Vacancy\\Files\\3\\710d7a38-5a4d-44fc-883f-5996150ba507.jpg";
Image2.ImageUrl = "C:\\Users\\naresh\\Desktop\\710d7a38-5a4d-44fc-883f-5996150ba507.jpg";
Second one Worked but first one didn't. I think the problem is with the spaces in the path. How can I resolve this problem?
Why do you not use a relative path to the location of your site? The path will be shorter and when you will port the web site on production server the image will still work (if the image is port to the server)
May be with the Vacancy which you have given two times. Check it, if its the mistake.
You can try to replace the spaces with %20.
But I wonder if that's the problem because the image is located at your local disk. Is this webpage only running on your local machine? Does the image exist?
Put quoted quotation marks into the string, e.g.:
Image1.ImageUrl = "\"C:\\Users\\naresh\\documents\\visual studio 2010\\Projects\\Vacancy\\Vacancy\\Files\\3\\710d7a38-5a4d-44fc-883f-5996150ba507.jpg\"";
Try giving the virtual directory path (create a virtual directory for the image folder. If it is in the root itself use the http://systemName/virtualDirectoryName/ImageFolder/image1.jpg) instead of physical path as follows:
Image1.ImageUrl=http://systemName/virtualDirectoryName/ImageFolder/image1.jpg
Hope this helps..
You are doing anyway very bad things in there. You should use Server.MapPath to resolve dynamically an IIS relative path into the file system path and you should not, never ever, load an image from the user's folders.
Put the image inside a subfolder in your web application, like for example create a folder called images at the same level where your aspx pages are.