C# - Get Image path from Client Machine - c#

I want to get the path of the Image which is saved in Client Machine. I know the Path and the file name of the Image. By using FileUpload i can do it, but without using fileupload is it possible to get the path of the file ??.
My Scenario is given below,
Public void ConverttoByte()
{
//Get the image path from web.config & this image is in client machine
string strConfig = #"C:\Manikandan\image\image1.jpg";
MemoryStream MS = new MemoryStream();
Byte[] data;
int fiFileSize;
System.Drawing.Image image;
image = System.Drawing.Image.FromFile(strConfig);
image.Save(MS, System.Drawing.Imaging.ImageFormat.Gif);
data = MS.ToArray();
CallDBMethod(data);
}
Here I converted the image as Byte and I called the CallDBMethod to insert this byte details to DB..
This image is available in client machine, but not in server machine..
So, how to i get this image path from client machine & how can i solve this?

Without the File upload control, It is not possible unless you create an Activex control (may not work in all browsers. User has to give permission to run this control).
In a web application, You can not take ( steal) any file from the user's computer without them making an action to do so( ex : selecting the file in the File Input control and clicking some upload button).
If you want the full file path of the file user selected in the file upload control, you can get by HttpPostedFile.FileName property which gives you the fully qualified name of the file on the client (Ex : C:\MySomeFolder\SomeFile.jpg).
string fullPath=FileUpload1.HttpPostedFile.FileName
Assuming FileUpload1 is the ID of the File Upload control.

According to my knowledge it is not possible. You can't get the client's image path. The browser does not allow us to get the path. Using fileupload control you can also not get the complete path.
A similar question by me
How to Get complete file path using file upload control in asp.net or any other way?

Related

File A replaced on server with B but File A is displayed

I've run into a problem in a situation where an image is deleted and another file with the same name is saved in its stead (i.e. the file is replaced). As shown below:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult _ReplaceGeneral(int ID, HttpPostedFileBase file)
{
var dbpath = query.FindImages(ID);
var path = Server.MapPath(dbpath.ImageURL);
System.IO.File.Delete(path);
file.SaveAs(path);
TempData["Message"] = "Image Successfully Replaced!";
return RedirectToAction("EditGallery");
}
In my server the file is successfully replaced. However, when I launch the site, the previous image is displayed.
Notes:
The Image isn't saved in my database- it's saved on my Server and the URL is stored on the database.
I can physically see the file replaced in the server so I'm absolutely positive it's being replaced.
Question: How and why does Image A display on my website even after replacing it with Image B?
I haven't been able to find any resources that articulate why and how this issue occurs so I apologize if it's a duplicate.
This sounds like a browser caching issue. If the URL of the image does not change the browser will use the cached version and not fetch the image from the server.
You can get round this by getting the image files last modified date/time convert that to a number and add it to the image URL as a query string (e.g. &t=XXXXX). In this way when the image file changes the URL changes too. The browser will then load the new image.

Saving a file in a WCF service to Images folder

So here is the problem. i am saving images in binary form to db. In the WCF service i created I want to save the file to a folder within the project i.e (/Images).
I normally use this code
//picbin is binary image data fetched from db
ImageConverter ic = new ImageConverter();
System.Drawing.Image img = (System.Drawing.Image)ic.ConvertFrom(picbin);
img.Save(HttpContext.Current.Server.MapPath(imagePath + picture_id + ".Jpeg"), System.Drawing.Imaging.ImageFormat.Jpeg);// imagePath ="Images/"
The problem is HttpContext.Current.Server.MapPath does not work in WCF services. so how can I get the physical path to save the image. Also what Url can i Get to access the image
I tried Using HostingEnvironment.MapPath, but the path it returns is always null.
You can use HostingEnvironment.MapPath.
please check below link
https://stackoverflow.com/a/10384987/2699211
img.Save("website absolute path/" +(imagePath + picture_id + ".Jpeg"),
you need to provide the absolute path. it works on my side.

Creating Image object using System.Drawing.Image.FromFile

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?

Accessing image from other server

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

Get the full path from a file upload

When I'm using the file upload control I just get only the file name, but I want to get the full path of the file location.
How do I get the full path from the file upload control in ASP.NET?
This is not possible in any browser, as a security measure.
If this were possible, an attacker could gain information regarding how files/folders were structured on a client computer.
Why do you need this information?
You cannot get it because the browser does not send it. It would be dangerous if the browsers sent the full path at user's system.
If you are using the ASP.NET upload control, on client side you can get the full path like the following.
document.getElementById('UploadControl').value
On the server side,
UploadControl.PostedFile.FileName
Check the MSDN article HttpPostedFile.FileName Property for more information.
I think you got file path of the upload control
HttpPostedFile httpBrowseFile = FileUpload1.PostedFile;
int FileLength = httpBrowseFile.ContentLength;
byte[] myData = new byte[FileLength];
httpBrowseFile.InputStream.Read(myData, 0, FileLength);
FName = path + FileUpload1.PostedFile.FileName.Substring(FileUpload1.PostedFile.FileName.LastIndexOf('\\') + 1);

Categories