unable to load a local resource OR virtual path error - c#

Situation
I have two applications that both read and save images / documents to one specific folder. Example if the user uploads their image in one program, a user of program B is supposed to view and even edit that image.
What I have done
In one of my application I have created a setting in settings for the string of my path. So to save an image path I call
path = System.IO.Path.Combine(Properties.Settings.Default.imagePath, Fimage);
this works fine.
However, my issue is when I try to view the image in my edit view.
Controller
ViewBag.imagePath = Properties.Settings.Default.imagePath;
View
<img src="#ViewBag.imagePath/#Url.Content(#Model.image)" alt="Image" style="height: 255px; " />
Problem
The problem is upon attempting to view the image I get the error, Not allowed to load local resource: I have full access to the folder and when I attempt to browse to the file in the error message the picture is displayed. I was advised from other questions to use, server.MapPath however when I do Server.MapPath(Properties.Settings.Default.imagePath); I get the error physical path received, expected virtual path.
This viewbag holds the entire string of folder my files are stored with the EVERYONE user having full access. So i'm really unsure of why it's making a fuse.
P.S I cant say something like "~/content/images" because the path to that file is in an entirely different application, I think I need to give it the entire location.
Any assistance will be appreciated.
Gratitude

Looks like your defaultImage variable holds a physical path, which will work for saving the image since the API requires a path in the form e.g. c:\website\images\.... but for viewing, you need the VirtualPath, something of the form http://mycoolsite/images/image.jpg and this is what the error is telling you.
Your Controller; therefore, should return something like
ViewBag.imagePath = Properties.Settings.Default.imagePath.Replace(Request.ServerVariables["APPL_PHYSICAL_PATH"], String.Empty);
In order to get the Relative Path. See this other related question.
EDIT I just saw your "PS..."
If that's the case, then you need to pass the full URL to the other application: (e.g. http://someothersite.com/images/image.jpg).

Related

Saving images from IP Camera to folder in application

I have a camera connected to a network that provides images which can be accessed by IP. For example something like this.
http://170.1.2.3/image?camera=2
I get returned a jpg file, which I can easily display back on a webpage using normal html.
<img src="http://170.1.2.3/image?camera=2" />
But instead of simply showing the camera image as seen from the time of the page load, I also want to save this image locally to the server using an MVC4 application. In addition it would be nice to include a timestamp on the filename. I'm not having any luck finding an example that looks right to me.
Let's say I want to start with the most basic example:
string imgUrl = "http://170.1.2.3/image?camera=2";
savelocally(imgurl, "newfilename-"+DateTime.Now.ToString());
.. and savelocally() should place the file in "~/Uploads". How do I do this? All the examples I've come across have been for uploading a file from desktop. I don't know how to translate that into grabbing a remote image.
Update:
This is an incomplete answer, as pointed out by comments.
string url = "http://170.1.2.3/image?camera=2";
string localFilename = #"C:\Projects\MvcApplication1\MvcApplication1\Uploads\tofile.jpg";
using (WebClient client = new WebClient())
{
client.DownloadFile(url, localFilename);
}
It works, however I don't like the fact that the file path is an absolute one. I tried replacing localFilename with ~/Uploads/tofile.jpg but it results in an error on execution. If possible I want to avoid having a fixed location for the upload folder.

Shared layout priority

I have _layout1.cshtml in Areas/Book/Views/Shared/_layout1.cshtml and in /Views/Shared/_layout.cshtml.
Areas/Book/Views/Home/Index.cshmtl:
#{
Layout = "~/Areas/Book/Views/Shared/_layout1.cshtml";
}
Somehow it stills show the content of _layout1 in Root /Views/Shared instead of in the Areas/Book
Is there any priority when the layout view share the same name?
The code as you posted it here should work. I am using the same successfully here.
Make shure the path is correct and the layout file is where you say it is.
Maybe odd, but what could cause this if the file is there:
If your APP is running with a differen user which does not have access rights to the file it could happen that the file "is not there" for the app.

Using a php script to store an image in a specific folder on a server

In my application I'm saving an image and transferring to a server through the use of a php script whose sole job is to pass this image to the server which saves it in the root of the server.
The code I have for my upload is as follows:
NameValueCollection nvc = new NameValueCollection();
nvc.Add("id", "TTR");
nvc.Add("btn-submit-photo", "Upload");
UploadToServer.HttpUploadFile(Settings.Default.ServerAddress , sfd.FileName.ToString(), "file", "image/jpeg", nvc);
Settings.Default.ServerAddress holds the location of my upload php script which the following:
http://server.foo.com/images/upload.php
I have another php script that returns a string of all the file and folders held on my server that is returned and displayed in a text box.
I'm calling this like so:
using (var client = new WebClient())
{
result = client.DownloadString("http://server.foo.com/images/getDirectoryList.php");
}
What I need to do is have a way so that I can choose the location of where the my image is stored. My feeling is telling that I need to do is have a way so that when a user selects anything with .folder, the old string list disappears and the images and files is stored within that folder.
I believe the call I need to make is something like this:
http://server.foo.com/images/getDirectoyList.php?dir=test_folder
But I'm stuck on trying to implement what I want. For one, the list I get back is all highlight and say I get something like
Image 1
Image 2
Test_folder.folder
I have no way of being able to simply click on Image 2 and have it highlight the whole thing. Instead is simply places the cursor where I clicked. Likewise I have no idea of how I would pass this information over to the my upload code so that my chosen directory is used to store the image as opposed to the root.
Has anyone ever attempted something like this before?
Do you have any links or advice that could help me achieve what it is I want to achieve?
Additionally, but not important, would I be able to ever create a new directory / folder on my server through my C# winform application without having to touch either the php scripts or the server itself?

How to read file in C# from POST data from web

Basically, I'm building a website that allows user to upload file.
From the front end (JavaScript), the user will browse a file, I can get the site to send POST data (the parameter "UploadInput" and it's value, which the value is the file)
In the backend (C#), I want to make a copy of the file and save it in a specific path.
Below is the way I did it.
var files = Request.Files;
file[0].SaveAs("\temp\\" + file[0].FileName);
The problem I ran into is that I got the error message saying index out of range. I tried Response.Write(files.Count) and it gives me 0 instead of 1.
I'm wondering where I did wrong and how to fix it, or if there's a better way of doing it.
Thanks!
Edit:
I am using HttpFox to debug. From HttpFox, I can see that under POST data, parameter is "UploadInput" and the value is "test.txt"
Edit 2:
So I tried the way Marc provides, and I have a different problem.
I am able to create a new file, however, the content is not copied over. I tried opening the new created file in notepad and all it says is "UploadInput = test.txt"
If they simply posted the file as the body content, then there will be zero "files" involved here, so file[0] will fail. Instead, you need to look at the input-stream, and simply read from that stream. For example:
using(var file = File.Create(somePath)) {
Request.InputStream.CopyTo(file);
}

Htmlfile access is denied when using Server.MapPath

I have been using Server.MapPath("page.aspx") for quite a long time, but it is just now that I faced this problem.
Basically here is my code
Session.Clear();
ShowLoggedOffControl(); //A function that setup bunch of controls visibility
OnUserLoggedOut(new EventArgs());
Response.Redirect(Server.MapPath("~/Default.aspx"));
The error would be htmlfile:access is denied at javascript execution. However when I removed Server.MapPath so that it became like this Response.Redirect("~/Default.aspx");, things work normally.
What did I do wrong? Why, how and when can I use Server.MapPath?
Thanks.
Server.MapPath maps the specified relative or virtual path to the corresponding physical directory on the server. So in your example it would end up redirecting to something like this:
c:\Projects\MyWebsite\Default.aspx
which is probably not what you want.
Response.Redirect on the other hand will resolve the '~' to the relative path root for you and resolve to something like this:
/MyVirtualDirectory/Default.aspx
As for when you would want to use Server.MapPath, you would use it if you wanted to actually find the file on the server and do something such as:
var lines = System.IO.File.ReadAllLines(Server.MapPath("~/MyTextFile.txt"));
// Do something here with values found
Server.MapPath gets the physical path to the file on the hard disk, whereas Response.Redirect expects a URL.
If for some reason you need to get the full URL, you can use this:
String.Format("http://{0}{1}", Request.Url.Host, Page.ResolveUrl(relativeUrl));

Categories