I have a logo that I need to show inside a mail. On my local machine, the logo is shown, but on the server where the live environment is, the logo is not shown.
I'm using the path like this:
imagePath = "~/Images/email-logo2.png";
The email-logo2.png exists in the same folder on the local machine and on the server, too.
I have tried to add permissions to read in the server folder where the png exists, but it did not resolve the problem. Can you advise?
The image is added in the email like this:
HTML:
<img class="auto-style4" src="{PictureSrc}" /><br />
C# code:
switch (property)
{
case "PictureSrc":
string imagePath = "";
if (User.Identity.GetUserId<int>() == 3140 ||
User.Identity.GetUserId<int>() == 3142)
{
imagePath = "~/Images/email-logo2.png";
}
else
{
imagePath = "~/Images/email-logo.png";
}
content = content.Replace("{" + property + "}", HttpContext.Server.MapPath(imagePath));
Server.MapPath returns a path on disk (e.g. C:\images\image.png), not a URL. See https://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath(v=vs.110).aspx.
So a user viewing the email from another machine will obviously not be able to resolve a path on the server's disk, it has no access to that disk.
The image path you provide has to be a fully qualified URL e.g. http://www.example.com/images/image.png.
It worked locally because you're on the same machine as where the image is located, so it happens to have access to that path, but that's not true for everyone else using it.
Alternatively, if the image is not too large you can convert it to base64 and embed it into the HTML in the email.
Related
I am working on uploading a file from MVC and I want to store the uploaded images on the network or any other system,I have IP address of that system,
Can anyone help me to find out where I am making mistake or is there some other way to store image on certain IP address.
I am using this code to save image on another machine with IP address
public ActionResult Test(string ENROLLIMAGE)
{
if (!string.IsNullOrEmpty(ENROLLIMAGE))
{
HttpPostedFileBase file = Request.Files["ENROLLIMAGE"];
Guid ImageId = Guid.NewGuid();
var filename = ImageId.ToString() + Path.GetExtension(file.FileName);
file.SaveAs(Server.MapPath("\\\\192.168.11.113\\D:\\UploadedFiles" + filename));
Uri addy = new Uri("\\\\192.168.11.113\\D:\\UploadedFiles" + filename);
}
return View();
}
Kindly help !!!!
It is giving me error while I am trying to upload that "The given path's format is not supported."
Thanks
You can't use a drive specification like D: in an UNC path. What you should do is make a network share of the UploadedFiles folder on the remote machine, and use file.SaveAs("\\\\192.168.11.113\\UploadedFiles\\" + filename);
D: is not allowed in an UNC path, and will give you the error.
So, either use file.SaveAs("\\\\192.168.11.113\\<ShareNameHere>\\" + filename), or use file.SaveAs("\\\\192.168.11.113\\D$\\UploadedFiles\\" + filename) if you have administrative privileges on the target machine.
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.
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
Hi I am developing an asp.net web application. I have to access one of the image in images folder in root directory. I am using following code in my code behind file.
string imageDirectory = HttpContext.Current.Server.MapPath("~/images/");
string imageUrl = imageDirectory + "/img1.bmp";
This works fine in my local machine. My question is does this code work when I move my application to production ?
It should as long as you have an application root/virtual directory for your site.
Also, you can combine these two lines into:
string imageUrl = HttpContext.Current.Server.MapPath("~/images/img1.bmp");
If you're thinking of putting imageUrl into an <img> tag, then no, it won't work. Server.MapPath will return your file or directory as a local Windows file/directory name, so something like "C:\WebRoot\MyWebApplication". If you send this to the browser, obviously, the browser won't pick up the image.
What you can do is something like:
string imageUrl = ResolveClientUrl("~/images/myImage.gif");
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");