Asp.net upload image doesn't work correctly - c#

I am currenlty stuck on a small project. Here is my code;
String StuPicc = Server.MapPath("images/" + UploadPic.FileName);
UploadPic.SaveAs(StuPicc);
UploadPic is the control name. But, neither it display the name nor upload the selected image.
When i output the result on a label it display;
images/{image name isn't showing here}
Any help regard to this will be apreciatable.

If you are using a file upload control then..FileUploadControl ID="FuImage" file upload control..and uploaded files will be saved in "UploadedFiles" folder.
string path = "\\UploadedFiles\\" + Guid.NewGuid() + FuImage.FileName;
FuImage.SaveAs(Server.MapPath(".") + path);
Hope this helps...

Are you using the FileUpload control? I believe the filename is available from the inputfile property then, although the only code I have used is to retrieve the stream:
System.Drawing.Image i =
System.Drawing.Image.FromStream(flUploader.PostedFile.InputStream);
flUploader.PostedFile.Filename should be valid along with the input stream if the upload is occuring.

Related

How to manage my directory's in asp.net using fileupload

Please take some of your time and read my problem
I need by using fileupload to upload just images to my web application
But when I select the image and click on upload button if the program see it a jpg file it will create a directory with the name jpg an save it there
That goes to gif and png
Please any one if you could give the code I will be very very thankful to you
Or at least link could help me
string folder = System.IO.Path.GetFileNameWithoutExtension(FileUpload1.FileName);
string path = Request.PhysicalApplicationPath + "/" + folder;
if (!System.IO.Directory.Exists(path))
System.IO.Directory.CreateDirectory(path);
string server_path = Request.PhysicalApplicationPath + "/myfiles/";
FileUpload1.SaveAs(server_path + FileUpload1.FileName);
You can use System.IO.Path.GetExtension(FileUpload1.FileName); to get the extension of the file.
Physical path for the virtual directory using:
System.Web.Hosting.HostingEnvironment.MapPath(virtualDirectoryPath)

Opening a PDF or Word Document via a button click

I am developing a KnoweldgeBase/Library in which a page lists PDF's and Word Documents associated with the topic selected. These files are uploaded into a folder the URL being "/Interface/AdminUploads/Miscellaneous/FILENAME".
I am listing the files via a table in which each row has an image of the file type, then the file title and then the date published (all created via another page). How can i have the PDF or Word documents opening when i click on the image for the document?
Try This Code.
string filepath ="/Interface/AdminUploads/Miscellaneous/FILENAME"; // Full Path of Pdf.
WebClient client = new WebClient();
Byte[] buffer = client.DownloadData(filepath);
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", buffer.Length.ToString());
Response.BinaryWrite(buffer);
Response.Flush();
Response.End();
You need to put the path in the url of the file.
You need to provide the full path you can ur Server.MapPath to get the full path.
Some useful links
How to open a file by clicking on HyperLink
Try the simple code
Response.Write(string.Format("<script>window.open('{0}','_blank');</script>", "pdflocation/" + "Example.pdf"));
I got it to work via...
System.Diagnostics.Process.Start(#fileLocation);
Running this code in a method on image click and passing in the variable 'fileLocation';

C# - Get Image path from Client Machine

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?

image control unable to display image

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.

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