1

I am using FileUpload control to handle the file in following scenario, I have downloaded some files manually in my machine, now I need to select files one by one to process their data in Database and Map those files to another location (server). I have a method which do this functionality, which needs fully qualified path of that file to further process it. I have used fileUpload.PostedFile.FileName which is only returning file name, not FQN.

if (fileUploadList.HasFile)
            {
                string source = ddActiveList.SelectedItem.Text;                
                string url = fileUploadList.PostedFile.FileName;
                string name = "Name";
                string id = "1";
                bool res = SaveFileToLocalMachine(url, name, id, source);
            } 

private bool SaveFileToLocalMachine(string url, string fileName, string sourceId, string source)
        {
            bool flag = false;
            lSource = new Object();
            string savePath = "@\\myPath";
            if (!Directory.Exists(savePath))
            {
                DirectoryInfo di = Directory.CreateDirectory(savePath);
            }
            using (var client = new WebClient())
            {
                try
                {                   
                    string fileExt = System.IO.Path.GetExtension(url);
                    string file = fileName + fileExt;
                    client.DownloadFile(url, savePath + file);
                    lSource.Update();
                    flag = true;

                }
                catch (Exception ex)
                {                    
                    flag = false;
                }
            }
            return flag;
        }
Kiran Joshi
  • 1,758
  • 2
  • 11
  • 34
Abdul
  • 1,416
  • 9
  • 26
  • 57
  • 3
    Why do you want to know the path? Only Internet Explorer provides it (which it really shouldn't), and it is useless to you anyway (since it gives you the path on the client PC - which the web server doesn't have access to)? – mjwills Jul 03 '18 at 12:04
  • @mjwills I need to provide that to my function which actually saving that file to machine with modification – Abdul Jul 03 '18 at 12:06
  • You need to change your server-side logic then. Browsers don't provide it (other than IE). There is no way to work around it. – mjwills Jul 03 '18 at 12:08
  • Then how would I accomplish a task which require such conditions ? any suggestions – Abdul Jul 03 '18 at 12:08
  • 1
    @trighati: you haven't got it. A webserver normally can't access files and folders of a client. The client provides the file to the server, the server doesn't download it from the client. So your logic is broken. You have the file already in `fileUploadList.PostedFile`. – Tim Schmelter Jul 03 '18 at 12:10

1 Answers1

2

A webserver normally can't (and shouldn't) access files and folders of a client. He should not even get the file-system of the client (which is what IE allowed some time). But you should not need it anyway because you have already the file uploaded from the client. It is in FileUpload.PostedFile.

The method that you want is already available, FileUpload.SaveAs:

fileUploadList.SaveAs(savePath);
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Aren't there like at least fifty duplicates that contain basically this answer? – CodeCaster Jul 03 '18 at 12:19
  • @CodeCaster: yes, now i've found one – Tim Schmelter Jul 03 '18 at 12:22
  • And not to mention that many browser vendors stopped sending the file path since it's of no use to you from a web server since you have no connection to the client. If you do attempt to build a process on the file path then you're going to have a brittle process since at some point they will all stop sending this information. – Mark Fitzpatrick Jul 03 '18 at 12:22