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

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" />

Related

How to create a hyperlink a new ASP.NET web form in a Webapplication

I am trying to add a new web form to an ASP.NET web application. However, wen I add a hyperlink to it from my main page, I get an IIS error. I am running it on localhost, i.e. my own machine without a web server. The hyperlink is:
Products
I have a products folder and a products.aspx file in it.
My Solution Explorer window:
This is the IIS error I get:
The "~/Products" points to a directory rather to a file, thus your IIS complains that it is not allowed to show the contents of the directory.
Point to the file instead
Products
You need to add runat="server" as well to resolve the dynamic starting location.
Products
This is needed so that ASP.NET CLR engine can dynamically update the path prior to delivering the final HTML code.
I read that you moved Products.aspx page to the root of your site, but you're not able to display the page.
Try replacing the existing code with this. And make sure runat="server" is included.
<a runat="server" href = "~/Products">Products</a>

Get VB.NET Relative \Upload path to the running application?

I found my trouble spot I think may be only because I am running the app on localhost, but I dont feel I can safely deploy to the next tier to test.
I have a Telerik RadAsyncUpload control that has a TargetFolder="\Upload" and was assuming all along it would use the \Upload folder in the application.
Is there a way to force the path to be inside the application?
lsFullFileName = HttpContext.Current.Server.MapPath(fuUploadFile.TargetFolder)
+ "\" + Path.GetFileName(lsFileName)
This is in VB.net. and the file is ending up in "C:\Upload" (which I created to see what was going on) but the file should be moved from its temp location in App_Data to
"C:\applicationName\applicationName\Upload"
ASPX
<telerik:RadAsyncUpload
runat="server"
ID="fuUploadFile"
MaxFileSize="262144000"
OnClientFileUploaded="OnClientFileUploaded"
InputSize="50"
TargetFolder="\Upload"
AllowedFileExtensions=".wav,.mp3,.mpeg,.mpg,.wmv,.avi,.mp4">
</telerik:RadAsyncUpload>
Thanks.
If you're insistent on keeping your upload directory within your site folders, then you can use a path like ~\Upload which will force the path to start from the web application root.
However, this is generally a bad idea as Bartdude outlined.
Try this:
uploadPath As String = AppDomain.CurrentDomain.BaseDirectory & "Upload"

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 which is stored in local drive?

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"

Resources