I have a folder C:\Images which has a some images. This folder is not inside my project and I would to know if there is a way to load an image from that folder on to an ASP.NET Image control.
<asp:Image ID="img" runat="server" />
One solution could be to make the Images folder a Virtual directory on the IIS but I would like to know if this can be done without creating a virtual directory for the Images folder.
Assuming proper access is granted to the Images folder you could do something like this:
Your main page:
protected void Page_Load(object sender, EventArgs e)
{
mainImage.ImageUrl = "ImageHandler.ashx?image=MyImage.jpg";
}
ImageHandler:
public void ProcessRequest(HttpContext context)
{
byte[] imageBytes = File.ReadAllBytes(#"C:\Images" + context.Request["image"]);
context.Response.ContentType = "image/jpeg";
context.Response.BinaryWrite(imageBytes);
}
At the end of the day, the image needs to be sitting somewhere that the user's browser can see it.
That means your options are:
Move the image to a directory on the webserver
Set the image's directory up as a Virtual Directory
Copy the image at runtime to a directory on the webserver
The Best Practice answer is to put the images into your project if they're part of the site, or to map a Virtual Directory if the directory is a store of user-uploaded images.
Related
I have a UWP application where i have one folder which has multiple image files and HTML page which renders all images. This images are downloaded based on the listview item selections. I am using WebView control to render HTML page in my app.
I am able to display the HTML page loads correctly along with all JS and images, but the problem is that it shows the same images even though the images in the folder changed.
Here is how i am loading html page
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
if (e.Parameter != null)
{
webView360.Navigate(new Uri("ms-appx-web:///local/Image360Viewer/index.html"));
}
}
For example, Load only simple HTML with "Hello" in body then change to "Hello World" from text editor when application is still running then navigate to your view again, it should show "Hello World" but it will still show "Hello" which was the version when application started.
Edit: I have HTML and image in project folder structure.
Where're the images and HTML file? If these files are in app local folder. You need to use the Uri like this: "ms-appdata:///local/Image360Viewer/index.html"
The ms-appx-web URI scheme refers to a file that comes from your app's package, not app local folder.
Please read URI schemes for more information.
I have a aspx page to upload image to a folder.The aspx page is inside a folder called admin.
If i keep the aspx page in the root directory i am able to upload file in the Image folder since both are in the root directory.
But when i do the same keeping the aspx page inside a folder,it shows Could not find a part of the path 'C:\Users\...
So how do i provide path such that i am able to upload image in a folder from a page which is also inside a folder.
Here goes my code:
if( fu_subcat.PostedFile.ContentLength>0 )
{
fn =Path.GetFileName(fu_subcat.PostedFile.FileName);
fu_subcat.SaveAs(Server.MapPath("imag/" + fn));
}
EDIT
It is mapping the path as:
Could not find a part of the path 'C:\Users\lenovo\Documents\Visual Studio 2010\WebSites\emarket\Admin\imag
But i have the folder inside emarket as emarket\imag not emarket\Admin\imag
Best to keep the image in a separate content folder, and then reference the image in a relative path, using the tilda e.g ~"content/image"
Using ~ will get you to the virtual root of your application, so Server.MapPath("~/imag/...") should do it.
This happens because when you call that code from
/Admin/page.aspx
Server.MapPath returns a path which is relative to /Admin/ - so the final path would be /Admin/imag/... To set path from the root you should add a / in front of the path.
Must be
fu_subcat.SaveAs(Server.MapPath("/imag/" + fn));
Notice / in front of the string.
Alternatively you can also add a ~ tilde
fu_subcat.SaveAs(Server.MapPath("~/imag/" + fn));
which points to a root of the current ASP.net application. This will be useful if you application is running in a virtual directory and as a part of global application.
Here is my image control in UI:
<asp:Image ID="Image1" ImageUrl='<%# Bind("strCLogo") %>' runat="server" width="200" Height="200"/>
And here is the C# code, I am grabbing the link of the image from database (customerstbl) but unable to bind it with image control, I can see the image link in the source but its not showing :
protected void Page_Load(object sender, EventArgs e)
{
int nCID = Convert.ToInt32( Session["nCID"].ToString());
DataClasses1DataContext _dc = new DataClasses1DataContext();
CustomersTbl _customer = new CustomersTbl();
_customer = _dc.CustomersTbls.Where(a => a.nCID == nCID).FirstOrDefault();
Image1.ImageUrl = _customer.strCLogo.ToString();
}
here is how I am saving the link of the image in database, It seems I have an issue when I save the image link in database (it saves the whole path not the local directory path and image name)
string s = Server.MapPath("~/Imageslogo/") + Path.GetFileName(LogoUpload.PostedFile.FileName);
LogoUpload.SaveAs(s);
Save the image path as a relative path, configure root path globally
You are storing the path to the image as an absolute path. This is bad for several reasons, the main ones being:
What happens if you deploy your app to a different server with a different file structure? your absolute path will be wrong
What happens if you decide to move all of your images (but keep the structure of the image folder)? Again, your absolute path will be wrong.
I would advise you to store the relative image path in the database. In this case /Imageslogo/100001203240.jpeg. Then store the image root path (the part before the relative path) in your Web.config file. E.g. in the appSettings:
<appSettings>
<add key="myApp.Imageroot" value="F:\projects\accounting\Accounting2014\Accounting2014" />
</appSettings>
You can get this appSetting value using the following code:
string myRoot = System.Configuration.ConfigurationManager.AppSettings.Get("myApp.Imageroot");
With these two parts of the full path, you can:
Use the relative path in your HTML output which will work fine on your web server
Construct the full path by combining the root from your appSettings with the relative path in the database. You might use this is you need to manipulate the image programmatically or otherwise access the actual file instead of just passing the path to the client browser.
Easily change the root by changing the appSetting
note
As an aside, if you are setting the ImageUrl property in code behind, then the ImageUrl attribute in your .aspx becomes pointless. So remove it:
<asp:Image ID="Image1" runat="server" width="200" Height="200"/>
F:\projects\accounting\Accounting2014\Accounting2014\Imageslogo\100001203240.jpeg is not the correct value for Image.ImageUrl Property, the correct value is the relative path like ~/Imageslogo/100001203240.jpeg.
You need to save the relative path to the database, use the code below to get the relative path
string imageLocation = string.Format("~/Imageslogo/{0}", Path.GetFileName(LogoUpload.PostedFile.FileName));
and save imageLocation to strCLogo column of CustomersTbl table.
my web page is uploading images to server folder which is 'd:\upresim'
I adding images with that code:
protected void Button2_Click(object sender, EventArgs e)
{
FileUpload1.SaveAs(Server.MapPath("~/image/a.png"));
Image1.ImageUrl = "~/image/a.png";
}
I have a selected image, that I receive from FileUpload that I have added from the selected image to server upresim into folder.
Afterwords I need to show the added image on Image1, but it show nothing what can I do?
You have to upload images under your web app folder, for that folder your app has to have rigths to write to filesystem, and then set relative url to Image control ImageUrl. You should also check if upload has a file, and preferably use file name from uploaded file.
For example, let's say that you have folder upresim in your web site root folder, then use this code :
if (FileUpload1.HasFile)
{
FileUpload1.SaveAs(Server.MapPath("/upresim/") + FileUpload1.FileName);
Image1.ImageUrl = "/upresim/" + FileUpload1.FileName;
}
I have image control.I want to load image from my specific path.
i have a code in page behind
string imagePath ="E:/DotNetProjects/Templates/Default/icons/Computer.png";
imgEditor.ImageUrl = imagePath;
imgEditor.AlternateText = "Unable To Find Image";
path is exist and image is also available but always load alternate text.
imgEditor is my image control ID.
Plz help to catch my mistake.Thanks.
Just put your image in solution(any folder or even in root) and path image uri from that (with src in asp page) like :
src="Templates/Default/icons/Computer.png"
The imagePath is a filesystem path... you need a URL... (something like http://...). The URL must be accessible from the browser i.e. you need to setup your webserver (IIS) to serve the respective path... I would recommend putting the image into the solution/project so that the URL is relative...