my code is:
string filename = FileUploader.PostedFile.FileName.Substring(fuImage.PostedFile.FileName.LastIndexOf("\\") + 1);
if(fuImage.HasFile)
{
FileUploader.SaveAs(Server.MapPath("Modules/NewUserProfile/UserPic/" + filename));
imgUser.ImageUrl = Server.MapPath("Modules/NewUserProfile/UserPic/" + filename);
}
imgUser is id of asp:Image Control.Image is uploaded in desire folder but its not display image in image control.What i am doing wrong here? Is there any postback issue.Thanks.
Use relative path for ImageUrl property.
imgUser.ImageUrl = "Modules/NewUserProfile/UserPic/" + filename;
OR use root operator ~ if Modules folder is located at root of web-app.
imgUser.ImageUrl = "~/Modules/NewUserProfile/UserPic/" + filename;
To run this code please understand the following things:
Server.MapPath() is used for getting physical Path like: D:/Img/Upload/..
so it is good idea to get path for saving image.
But in the case when you are getting the image for binding it to image control then you must have to use virtual path instead of Physical path.
Virtual path like: localhost/demo/upload/myimage.jpg.
I usually debug problems like this by first doing a view-source with your browser, and making sure the URL in the HTML source is what you expect. Then try copying the url that shows up in your view-source and pasting it into the address bar of your browser, and see if it shows up.
I think you should wait for the image to completely upload and then set the Image path to the image.
Also after rendering into the Browser use FireBug in Mozilla or Chrome Inspect Elemnt to check whether the Image has been rendered properly by looking at the image. If it broken then your image path is wrong. Try to give relative path and check.
newpic.ImageUrl = Page.ResolveURL("~/Pictures")+filename;
Server.MapPath returns physical drive location which is not useful when you are assigning to the imageurl.
Related
I am trying to get the image dimensions of an image that user selects from list box. Image files are available on FTP server. I am displaying file names in a list box for users to select. Upon selection, I want to show the preview of image, for that I want to get dimensions so that I can resize it if i need to.
I am storing file name that is linked to currently selected list item into a string variable. I know that path on the server. I am using following code to create the Image object, but having no luck
try
{
string dir = Session["currentUser"].ToString();
System.Drawing.Image img = System.Drawing.Image.FromFile("~/Uploads/"+dir+"/"+fName, true); //ERROR here, it gives me file URL as error message!
}
catch(Exception ex)
{
lbl_Err.Text = ex.Message;
}
Not sure what is going wrong. Any ideas?
use Server.MapPath to fetch the image from the server.
As follows
System.Drawing.Image img =
System.Drawing.Image.FromFile(Server.MapPath("Uploads/"+dir+"/"+fName), true);
You can use following as well
Server.MapPath(".") returns the current physical directory of the file (e.g. aspx) being executed
Server.MapPath("..") returns the parent directory
Server.MapPath("~") returns the physical path to the root of the application
Server.MapPath("/") returns the physical path to the root of the domain name (is not necessarily the same as the root of the application)
References
Server.MapPath("."), Server.MapPath("~"), Server.MapPath(#"\"), Server.MapPath("/"). What is the difference?
I have image file placed on the one server and application on other server.
I want to access that image, below code I have written:
On default.aspx, I have
<asp:Image ID="Image1" runat="server" ImageUrl= "GetImage.aspx?imgName=MyImage.jpg" />
and on GetImage.aspx, I have written the below code on page_load
protected void Page_Load(object sender, EventArgs e)
{
// Changing the page's content type to indicate the page is returning an image
Response.ContentType = "image/jpg";
var imageName = Request.QueryString["imgName"];
var path = "//SERVER/FOLDER/" + imageName;
if ((string.IsNullOrEmpty(imageName) == false))
{
// Retrieving the image
System.Drawing.Image fullSizeImg;
fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(path));
// Writing the image directly to the output stream
fullSizeImg.Save(Response.OutputStream, ImageFormat.Jpeg);
// Cleaning up the image
fullSizeImg.Dispose();
}
}
But I am getting error at
fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(path));
Please let me know where I am incorrect. Do I need to anything else other than Server.MapPath ? because my image is on other server.
EDIT
I have image folder in my computer
I have created a web app in other computer [same network], deployed
in IIS, image is displayed correctly. With path like
http://10.67.XX.XX/websiteName/Default.aspx
but when I am trying to access the same from my comupter or any other
computer, I am not able to see the image.
You shouldn't use Server.MapPath. This is used to map virtual paths under your site to physical paths under file system. If the file exists on another server, just access it by name directly, without Server.MapPath.
The answer above is incorrect because the images reside on a separate server.
So System.Images will not no where the image is.
Secondly you have to use server.Mappath for system images, it requires a windows path i.e. c:\blah\blah\whatever.jpg.
you will need to use \\server\C$\folder\image.jpg this works well with system.image.FromFile
Also saving you can also utilize this.
Cheers
I use asp.net 4 and c#.
I have this code that allow me to find the physical path for an image.
As you can see I get my machine physical pagh file:///C:.
string pathRaw = HttpContext.Current.Request.PhysicalApplicationPath + "Statics\\Cms\\Front-End\\Images\\Raw\\";
Result:
file:///C:/......../f005aba1-e286-4d9e-b9db-e6239f636e63.jpg
But I need display this image at the Front end of my web application so I would need a result like this:
http://localhost:1108/Statics/Cms/Front-End/Images/Raw/f005aba1-e286-4d9e-b9db-e6239f636e63.jpg
How to do it?
PS: I need to convert the result of variable pathRaw.
Hope I was able to express myself unfortunately I'm not sure about terminology in this case.
Thanks for your help!
The easiest thing to do is get rid of the physical application path.
If you cannot do that in your code, just strip it off the pathRaw variable. Like this:
public string GetVirtualPath( string physicalPath )
{
if ( !physicalPath.StartsWith( HttpContext.Current.Request.PhysicalApplicationPath ) )
{
throw new InvalidOperationException( "Physical path is not within the application root" );
}
return "~/" + physicalPath.Substring( HttpContext.Current.Request.PhysicalApplicationPath.Length )
.Replace( "\\", "/" );
}
The code first checks to see if the path is within the application root. If not, there's no way to figure out a url for the file so an exception is thrown.
The virtual path is constructed by stripping off the physical application path, convert all back-slashes to slashes and prefixing the path with "~/" to indicate it should be interpreted as relative to the application root.
After that you can convert the virtual path to a relative path for output to a browser using ResolveClientUrl(virtualPath).
Get the root of your application using Request.ApplicationPath
then use the answer from this question to get a relative path.
It might need a bit of tweaking but it should allow you to do what you're after.
Left-strip your pathRaw content by the Request.ApplicationPath and construct the url using
Uri navigateUri = new Uri(System.Web.HttpContext.Current.Request.Url, relativeDocumentPath);
Make use of
ApplicationPath - Gets the ASP.NET application's virtual application root path on the server.
Label1.Text = Request.ApplicationPath;
Image1.ImageUrl = Request.ApplicationPath + "/images/Image1.gif";
You can use Server.MapPath for this.
string pathRaw = System.Web.HttpContext.Current.Server.MapPath(“somefile.jpg“);
In my c# class I wrote I have a photo property that returns the photo source if the image exists (nothing or default image otherwise). In my code I use:
public string Photo
{
get
{
string source = "~/images/recipes/" + id + ".jpg";
if (File.Exists(source))
return "~/images/recipes/" + id + ".jpg";
else
return "";
}
}
If I get the FileInfo() information for this image I see that I tries to find this image in the following directory: C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\~\images\recipes
Of course the image is not located in that directory and File.Exists is returning me the wrong value. But how can I fix this?
Try this:
if(File.Exists(System.Web.HttpContext.Current.Server.MapPath(source)))
You need to map the relative path back to a physical path:
string source = HttpContext.Current.Server.MapPath("~/images/recipes/" + id + ".jpg");
You'll have to use:
Server.MapPath(source)
As you can not be 100% sure where the code will be running from, ie. it will be different in development and on a production server. Also are you sure ~/ works in windows? Wont that just be interpreted as a directory named ~? Unless thats what you want.
For some strange reason my picture is not loading at runtime:
string path = Server.MapPath("./abc.jpeg");
Response.Write("the path is:");
Response.Write(path);
img_ProfilePic.ImageUrl = path;
As you see from above code, I have verified that the path is correct.
Also the image is only 20 KB and is JPEG.
My environment is VS 2008 C#
Thanks
Server.MapPath returns the physical (file system) path.
Image.ImageUrl requires a virtual path (or relative/absolute URL). You should use it like this for example:
img_ProfilePic.ImageUrl = "~/images/abc.jpeg";
img_ProfilePic.ImageUrl = "../abc.jpeg";
img_ProfilePic.ImageUrl = "http://www.host.com/abc.jpeg";
More on web project paths (check the Server Controls section which is specific to your problem):
http://msdn.microsoft.com/en-us/library/ms178116.aspx
Right click on the "broken image" icon, and copy and paste the path into your browser. Do you get the image, a "broken image" or a 404?
Are you testing locally?
Replace string path = Server.MapPath("./abc.jpeg"); with string path = Server.MapPath("~/abc.jpeg");