How to display image which is stored in local drive? - asp.net

I have a asp page in which i have to display the image which is stored in my local disk C:
i.e..
C:\Program Files\Adrenalin\Adrenalin\UploadedFiles\TemplateFile\abc.jpg
how can i do that...i am not able to do so.
the image is not displayed instead it shows a empty image holder with the name of the image as specified and URL as not available.
Please help....

You will have problems with permissions if the image is outside of the web site folder. Traditionally, web sites run under the NETWORK SERVICE user account, which will limit access to files outside of the folder. You will need to extract the file from a folder with similar access and it is extremely unwise to do so, particularly from Program Files.
You should possibly proxy the file via a web page or web service, which doesn't expose the fact that the image is served external to the web site. You'll need to make sure the target folder C:\Program Files\Adrenalin\Adrenalin\UploadedFiles\TemplateFile has NETWORK SERVICE Read-access.
eg. create a blank ASP.NET page:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="ImageServer.aspx.cs" Inherits="ImageServer" %>
with the code behind:
class ImageServer
{
void Page_Load(object sender, EVentArgs e)
{
Response.ContentType="image/jpeg"; // for JPEG file
string physicalFileName=#"C:\Program Files\Adrenalin\Adrenalin\UploadedFiles\TemplateFile\abc.jpg";
Response.WriteFile(physicalFileName);
}
}
And test in your browser by going to the URL
http://<localhost>/<website>/ImageServer.aspx
You should get the image.
Then, within the tag, use the URL of the page as your image placeholder:
<img src="ImageServer.aspx" alt="Image served" />
UPDATE:
Looking at your latest comments, I suggest you send a QueryString parameter with some sort of employee code and use that to query the database and get the appropriate filename within the Page_Load() method. Don't send the filename as part of the QueryString.

What you're trying to do can work, but it's highly discouraged in Web development. Putting the permissions problems aside (which are very serious), you can never assume that your images will be available in the absolute path you provided on drive C:.
What you should do, is create a folder inside your website directory and use it to store the images, and use relative links instead of absolute links.
Even if you manage to make absolute links work now, prepare yourself for sever headaches in the future when running your application in different configurations.

copy the image in your project root folder and provide the image source as only the name of the image instead of the complete path.
e.g., img src = "battlefield.jpg"

Related

How to retrieve asp.net media / resources based on logged in user?

I have an asp.net web api project using token based authentication. my app uploaded and retrieve images and I keep file path in table_myfiles along with the uploaded user ID.
I would like the user to access only the files he have uploaded, which I can identify from the table.
How to protect my resources to restrict access to only to the user based on table_myfile ? And not to anyone without logging in or direct link / path ?
I have been searching for any possible solution for a week now , I think I should implement a middleware to manage access. But I couldn’t find any resources on how to implement the same.
Currently my api shows all resources just by directly accessing the file path/link.
The simple apporach is to remove the vitural folder, or that folders from the web site folders. That way, no simple URL exists for any of the files.
So, for a user to get/see/use/download a file? You present say a listview or some kind of grid (or repeater) that displays and lists out the files.
Then, when they want to download or view a file?
You use response.write and stream the file down to the client side.
Remember, on the server, code behind uses 100% clean and correct windows file paths. For any web based URL, then that folder must be in a valid path of the web site. When they type in a valid URL, it eventually gets translated to that given folder in the site (or a external folder provided when you create a mapped "virtual" folder in IIS. However, if you don't provide that virtual folder, or the folder is NOT in the web site file/folder sets, then no valid URL's exist. However, that folder can be directly used and hit with code behind - any valid server path/folder name is allowed in code behind.
Because when streaming the file, you need path name, file name, AND ALSO the "mine" type. Thankfully, .net 4.5 or later has this ability.
so, from a database (table) I display the file names like this:
But, if you click on the preview image, that is a image button.
The code behind simply gets/grabs the file name from the database.
I then download (stream) the file to the browser side like this:
if (File.Exists(strInternalFullPath))
{
string strConType = MimeMapping.GetMimeMapping(strInternalFullPath);
binFile = File.ReadAllBytes(strInternalFullPath);
Response.ContentType = strConType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(strWebUrl));
Response.BinaryWrite(binFile);
Response.End();
}
else
MyToast2(this, btnLink.ClientID.ToString, "File Not found", "We no longer have this file avaiable.");
so, this buttion behaves 100% like a link, but there are no existing URL's or path name that points to the files folder from a web based URL.
Remember:
Web based URLs - they auto map from the web site URL to a existing folder.
You can use server.MapPath("some url to file") to "translate" this to a internal file name.
Code based files:
In your .net code (code behind) ANY file name is a standard plane, jane file name that points to a file on the server.
so, once we have that file name from the database, you can steam the file as if the user clicked on a link. But you never have to expose the actual file name, or file path. And no such valid URL's exist on the web site, since you do NOT have that files folder in the web site folder hierarchy - but placed that folder outside of the web site.
As long as that folder is outside of the web folders, and as long as you don't setup a virtual folder that points to that folder outside the web folders?
Then code behind can STILL get/grab/see/use any file on the server. that code uses a full valid windows file name, - but the web site will have no mapping to such a folder - hence no valid URL's will exist or can be typed in.

What is the best way to show an image (which is in local machine) in picturebox

I'm working on a project in ASP.NET but I'm stucked in this case;
I have ten thousands of images in local machine. I need to show them in the picturebox in ASP.Net. I can not copy all images to web appliation folder. So I have to give the file path like
\\server\something\something.jpg
But it doesnt work of course.
I found some solutions but they are not so clear, so I didnt manage to success.
So what should I do ?
In IIS, define a virtual sub-directory in your site, with directory's physical location being \\server\something. Then you will be able to specify image virtual url as ~/something.jpg.
Use code-behind to return images (aspx page or ashx handler) - then, with enough permissions given to IIS user, the web application will read the required image and put it in response stream.

Problems with mediaElement.Source and absolute path

I have a SL project that is simply the MediaElement. I embed this in my aspx page and do all the controling of the element from the aspx page throught the java to managed code bridge. I had all my video files in the project as resources for testing but now want to move them out to the server where they will live. The server is in a different domain from my development box but I have access. I can see my files by going to the "run" command and typing that path as follows: "\mediaserver.otherdomain.com\Content\MyVideo.wmv" I can access the folder and view the video's fine from my computer. However, when I do my
`mediaElement.Source = new Uri(MediaPath, UriKind.RelativeOrAbsolute);`
in my code to load the video into Silverlight video does not play. The Media path is "\MediaServer.OtherDomain.com\Content\MyVideo.wmv. I have image files that I reference using the same path "\MediaServer.OtherDomain.com\Content\MyVideo.jpg" and they display fine.
When I debug and look at the values set in the .Source propery I see:
Absolute Path = "/Content/MyVideo.wmv"
Absolute Uri = "file://MediaServer.domain.com/Content/MyVideo.wmv"
Host = "MediaServer.domain.com"
This all looks good, so why don't I see the video?
Silverlight does not allow you to use cross domain references out of the box. You need to add an exception to do so.
If you're using SL4, look at Making a Service Call Across Domain Boundaries

How to display image that is located on another server on the network (ASP.NET)

I've got a ASP.NET site that's located on a local server (MY_SERVER). And one of the things it does is pull up tiff files which are located on another server (ANOTHER_SERVER). The location of each of these files is stored in SQL. I pull up each of these images and am supposed to display them. The problem is:
the files are not named with a tiff extension (does it matter?)
they aren't displaying at all.
I am using an Image control to display these images, and I'm not sure if it matters that the extension is not set (does the image control know the difference between an jpg and a tiff without the extension?)
I am guessing the images aren't displaying because they are not on the same server MY_SERVER that the images are located (ANOTHER_SERVER). Any ideas on how to fix this?
edit: actually displaying the tiff files were amazingly simple:
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "image/png";
new Bitmap(Request.QueryString["ImagePath"]).Save(Response.OutputStream, ImageFormat.Gif);
}
but because the images are located on ANOTHER_SERVER I still can't access them. I may just do a hack where I copy them to a local directory on MY_SERVER but there's gotta be a simple way to fix this. Anyone?
Are the images on ANOTHER_SERVER accessible via HTTP or are you trying to display them in an img tag using their UNC path?
Since web pages are viewed on client machines, the paths to resources (images/css/scripts etc) must be accessible from the client's machine. Even if they are accessible from the server, if they aren't accessible from the client they won't be viewable.
I suspect in this instance MY_SERVER can access the tiffs on ANOTHER_SERVER, however the path to ANOTHER_SERVER means nothing to the client accessing the page.
You will either need to read the image in from the disk and display it as an image using a customer handler, or expose the images on ANOTHER_SERVER via HTTP and reference them that way (which means the client must be able to directly connect to ANOTHER_SERVER).
Regardless of what tag you use, does ANOTHER_SERVER store the images in an internet accessible location? If not, you cannot serve them directly on the internet. You can try downloading the file with Response.WriteFile.
The <img /> tag is not meant to serve TIFFs. The QuickTime plugin is a free TIFF viewer, but it is not very flexible. Where I work, we use Atalasoft's dotImage, which converts TIFFs to tiled PNG on the fly, but it is not free. I found this CodeProject article. Even if you can transform the image into a web-friendly format, your server-side code might have to cache the file on the web server.
You should be able to display an image from another server if your img tag src is properly setup and the server is accessible. However, I don't believe that most browsers support directly viewing tiff files. There are a number of tiff viewers available (google it).
You might also try an <embed> tag and see if it does the trick (look here). The user's computer would have to know how to deal with tiffs.

Where does the ASP.NET development server get its images from?

In my web application I dynamically generate images and want to show the images as part of a web page.
But, when debugging using the ASP.NET development server (not IIS) - I have no idea where to store these images so they can be referenced from my web application.
Should I use Directory.GetCurrentDirectory()?
Or Assembly.GetExecutingAssembly().Location?
Or Assembly.GetExecutingAssembly().CodeBase?
None of these paths seem to work.
Any ideas?
Server.MapPath("~/") will give you the path the the root of the web application. Everything placed within the scope of this path should be automatically exposed by the web server, be it IIS or the VS devevelopment server.
The Server property is available in to all Page objects, and can otherwise be found on the HttpContext.
You can use
Server.MapPath()
You can write ashx handler to server images on the fly. Here is tutorial for this. It you want to generate and display them later then create a folder in your web-site folder tree and get its physical path using this:
string imgPath = Server.MapPath("/") + "Images\" + IMAGENAME;
Make a seprate aspx page, and put your code overthere that create your dynamic images..
your code should be look like....
Response.ContentType = dtblProductImage[0].ImageFileExt;//image extension
Response.BinaryWrite(dtblProductImage[0].ImageData); //your image binary
now go to that page where you want to show your image and set the image property of imageURL to your page, it will look like this....
<asp:Image ID="imgProduct" runat="server" ImageUrl="yourpage.aspx" />

Resources