so basically I am trying to piggy-back from this article https://www.c-sharpcorner.com/UploadFile/5357ed/importingreadingexporting-of-csv-file-in-Asp-Net/ so I can add upload functionality into my asp.net page and then customized my gridview to something else. Mainly, I have followed the same code and gave it a run to see if everything is fine but I am getting an error with the file path in btn_import_Click. It blows because it cannot find the directory. which is line 85 from the example.
"System.IO.DirectoryNotFoundException: 'Could not find a part of the path 'C:\inetpub\wwwroot\dir\dir\upload'.'"
I would really wish it would point to the physical location of the actual file since my users will have different machines and we all won't be using the same machine nor directories when they would have that .csv file.
If you guys could shine a light I would really appreciate it. I have done this in Java with no problem but I am having a hard time with this ASP.NET Project. Thank you in advance!
- Chiriqui
CodePudding user response:
I am confused here.
that file path name is the web server path name. So some web server being hosted some place? That has ZERO to do with the users local file system.
You cannot get, see, determine ANY path names on the client side computer running the browser.
When you up-load a file, you ONLY get the file name.
You cannot grab, get, determine, OR EVEN SET the file name from the client side.
I mean, if you hit my web site to view a cat picture?
The while you looking at the cat picture, my code can't start looking around on YOUR computer, and say grab a file called passwords.txt, or some files called my banking files.
When you up-load, then you NOW have the file, and can save it any place you want on that web server computer. But, as such, these path names, and even the file name you choose to use have ZERO to do with any folder name, or file name on the client computer side. the web works this way for reasons of security.
You have ZERO knowledge of path names, file names, or ANYTHING that is on the client side computer.
So a file upload control?
It lets the USER pick the file - not you, not your code behind, and not your JavaScript code. You can NEVER control, set, get, change, or even in code determine the drive letter, the folder, or even the file name. The user client side MUST and will choose the file. It is then up-loaded.
At that point, you are free to save the file in any folder. In that example, they used a folder called "upload". You can create and name and choose ANY folder name you want in place of the name "upload" folder. However, that folder is on the server side, and has ZERO to do with ANY name, and ANY folder on the client side.
You NEVER get passed the folder name when the user up-loads a file. You ONLY get a file name, and the file data. Nothing more, and nothing less is EVER set to your sever.
And what happens if they are using a Android phone, or a iPhone? They don't even have standard windows path names.
Now, the way this works might often be a bit confusing, since during development, your ONE computer is running both your code, has your file system, and has the web server all running on one computer.
However, once you publish that web site, then the code, the web server, and the folders and files used can ONLY be from the web server side. You have no information about the client side file system. Not even path names can be used, or even known about.
You get a file name, and you get the file data. At that point, you can save to any folder on your web server. but as such, these file names, path names, folder names are 100% under your control, and the end user has ZERO clue about what folders you as the developer choose to use for your web server.
So, the path name of "uploads" is NOT on the client side computer, but is a path name ONLY on the web server side of things.
So, it is thus confusing you state this:
the actual file since my users will have different machines and we all won't be using the same machine nor directories when they would have that .csv file.
But you can't know that location in your web server code. The user has to go and select that file, and the up-load to YOUR computer running your web server. Where you decide to save that file is YOUR choice, and not the end user choice.
You can NEVER look at, see, or know what folders the end user has. The ONLY task the end user is allowed with file-upload is to select a file on THEIR computer. However, when they do that, and up-load?
Then on your code behind and web server side of things?
You ONLY get a file name (no path information), and you ONLY get the file data. You can't change even the file name they select. After your server and code gets that file, then you can save the file into any folder on the web server. And you can even change the name of the file you save.
But, you can't change what file they selected, and you can't change were it comes from, and you can't even in JavaScript change or touch or modify the file the user chooses. As I stated, this is for reasons of security.
You have ZERO information about where the file was located. The ONLY information you get is a file name, and the file data. Zero additional information about that file the user picked can be had, or even determined by you.
You don't have ANY knowledge of the file path names, and folder names used client side. The only task a user is allowed is to pick a file. When the file picker is launched in the browser, the user (not you) can select a file from ANY folder. When they select that file, then the file name and data is sent to the server. But NOT the path name - only the file and data.
You have zero knowledge about the users path names. And your code and assumptions thus have to be based on this simple fact and limitation.
CodePudding user response:
Please don't chew me up. As I said, I am more familiar with Java at this point and some Asp.NET as far as doing this.
I found my issue based on another example: https://findnerd.com/list/view/How-to-read-CSV-file-in-asp-net/19762/
If you look on line 11 from the above link, they are using the following:
string path = Path.GetFullPath(filename.PostedFile.FileName);
This was it for me. I had it as
string path = Server.MapPath("~/Files/") Path.GetFileName(filename.PostedFile.FileName);
All I know that Server.MapPath did not work but Path.GetFullPath did. My program reads the file and the saves it to a DataTable. Then I bind the data table to the datasource of the gridview and voilá, I see the data from the csv file.
I wanted to come back and put an answer in case it's pertinent to another person running with the same situation. If someone else would be kind to explain the difference between Server.MapPath and Path.GetFullPath would be nice. That would add more explanation to this topic.
Thank you all and Regards! ^_^