Get the full path from a file upload - c#

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);

Related

How to find a file without specific path

Im trying to send a compressed file with an email.
So the step is :
1. Compressed the file that we want to.
2. Send the email in outlook, with the attachment from the compressed file.
My problem is when the application try to search for the compressed file, it wont find because my path didnt right.
Here's the code
using (ZipFile zip = new ZipFile())
{
//zip.UseUnicodeAsNecessary = true;
zip.AddDirectory(#"Y:\"+tglskrg+"\\Result");
zip.Save(#"C:\Users\Desktop\"+tglskrg+".zip");
}
string path = Path.Combine(Directory.GetCurrentDirectory(), tglskrg + ".zip");
//Send email code(which basicly work);
my problem is , the file is saved in desktop
the actual result is , when the apps try to search for the file, the apps look in the path directory which from the code i write, the path is in the Debug folder from the application.
Anyone can help ? or maybe where did i do wrong?
Thankyou
Maybe I am not understanding your question; but, if you want to refer to the desktop folder, use Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory).
For example, you are trying to save your zip file to the desktop,
change this:
zip.Save(#"C:\Users\Desktop\"+tglskrg+".zip");
to this:
zip.Save(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), tglskrg + ".zip"));

Upload to Google Drive using API C# from web user

Ultimately I'm trying to upload a document from the user's file system via MVC .NET web site to Google Drive, which utilizes a service account.
I'm not sure if I'm implementing the appropriate design to accomplish the upload but I am getting hung up on the path of the file to be uploaded.
Web
#Html.TextBox("file", "file", new { type = "file", id = "fileUpload" })
Controller
public ActionResult GoogleDriveList(GoogleDrivePageVM vm, HttpPostedFileBase file)
File _file = new File();
var _uploadFile = System.IO.Path.GetFileName(file.FileName);
byte[] byteArray = System.IO.File.ReadAllBytes(_uploadFile);
Error occurs on the ReadAllBytes statement. It could not find file 'C:\Program Files (x86)\IIS Express\Map of Universe.txt'. The file name is correct but the path is not.
byte[] byteArray = System.IO.File.ReadAllBytes(_uploadFile);
System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
... Google Drive file stuff goes here
Then upload the file from the stream.
FilesResource.InsertMediaUpload request = _service.Files.Insert(body, stream, body.MimeType);
request.Upload();
So, am I going down the right path using the HTML file helper? And if so, what's the trick to get the path to work correctly? Also, I want to be able to support file sizes up to 500 MB (if that makes a difference).
If your getting the filename from a user selected windows file explorer dialog, then you shouldn't be using the below as will just strip out the filename without the path into your upload file variable and I am assuming that bogus path is where your're running the code from, so ReadAllBytes is trying to read from that path with the filename
var _uploadFile = System.IO.Path.GetFileName(file.FileName)
just change so it has that full path and filename you need to use in ReadAllBytes
var _uploadFile = file.FileName

Using webclient to download images from deployed website

i deployed a website on IIS running on localhost/xxx/xxx.aspx . On my WPF side , i download a textfile using webclient from the localhost server and save it at my wpf app folder
this is how i do it :
protected void DownloadData(string strFileUrlToDownload)
{
WebClient client = new WebClient();
byte[] myDataBuffer = client.DownloadData(strFileUrlToDownload);
MemoryStream storeStream = new MemoryStream();
storeStream.SetLength(myDataBuffer.Length);
storeStream.Write(myDataBuffer, 0 , (int)storeStream.Length);
storeStream.Flush();
string currentpath = System.IO.Directory.GetCurrentDirectory() + #"\Folder";
using (FileStream file = new FileStream(currentpath, FileMode.Create, System.IO.FileAccess.ReadWrite))
{
byte[] bytes = new byte[storeStream.Length];
storeStream.Read(bytes, 0, (int)storeStream.Length);
file.Write(myDataBuffer, 0, (int)storeStream.Length);
storeStream.Close();
}
//The below Getstring method to get data in raw format and manipulate it as per requirement
string download = Encoding.ASCII.GetString(myDataBuffer);
}
This is by writing btyes and saving them . But how do i download multiple image files and save it on my WPF app folder? I have a URL like this localhost/websitename/folder/designs/ , under this URL , there is many images , how do i download all of them ? and save it on WPF app folder?
Basically i want to download the contents of the folder whereby the contents are actually images.
First, the WebClient class already has a method for this. Use something like client.DownloadFile(remoteUrl, localFilePath).
See this link:
WebClient.DownloadFile Method (String, String)
Secondly, you will need to index the files you want to download on the server somehow. You can't just get a directory listing over HTTP and then loop through it. The web server will need to be configured to enable directory listing, or you will need a page to generate a directory listing. Then you will need to download the results of that page as a string using WebClient.DownloadString and parse it. A simple solution would be an aspx page that outputs a plaintext list of files in the directory you want to download.
Finally, in the code you posted you're saving every single file you download as a file named "Folder". You need to generate a unique filename for each file you want to download. When you're looping through the files you want to download, use something like:
string localFilePath = Path.Combine("MyDownloadFolder", imageName);
where imageName is a unique filename (with file extension) for that file.

FiIeInfo.CreationTime. How to read correct value?

I have file upload control on .aspx page, I want to upload an image at a time whose location is not always the same, could be in same directory as .sln, on desktop, anywhere.
I am using this code to read the file creation time
string savePath = MapPath("~/" + Path.GetFileName(e.FileName));
FileInfo MyFileInfo = new FileInfo(savePath);
string dt = MyFileInfo.CreationTime.Day.ToString();
string mn = MyFileInfo.CreationTime.Month.ToString();
string yr = MyFileInfo.CreationTime.Year.ToString();
I have noticed one thing that
If the image is in the same folder as the website, it gives me correct values for all
dt , mn and yr
But if it's outside that location, it always gives me same value everytime
{01/01/1601 00:00:00}
Not sure how to sort this out?
any advice, helpful code? thanks
It seems you do not understand the client/server barrier yet. Paths from the client are not meaningful on the server.
It so happens that your website is running on the same machine as the client because you are debugging locally. That is just a coincidence and it allows your code to sometimes work (by coincidence).
Proper file uploading works by ignoring the path and reading from the stream provided by the file upload control. A file upload is just a stream of bytes to the server.

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?

Categories