0

I need to download a file from local folder using the partial file name,which is provided by the client side to the following function on Controller (RESTFUL API).

        [HttpGet]
        public IActionResult downloadTextFile(string partialName)
        {
            downloadTextFileFromLocal(partialName);
            return null;
        }

        private static void downloadTextFileFromLocal(string partialName)
        {
            //string partialFileName = "2018-10-3";
            DirectoryInfo hdDirectoryInWhichToSearch = new DirectoryInfo(@"D:\TextFile");
            FileInfo[] filesInDir = hdDirectoryInWhichToSearch.GetFiles("*" + partialName + "*.*");
            foreach (FileInfo foundFile in filesInDir)
            {
                string fullName = foundFile.FullName;
                Console.WriteLine(fullName);
            }
        }

Using this there isn't any error but the file doesn't downloads. How can i do this?

Amin Saadati
  • 719
  • 8
  • 21
Biraz Dahal
  • 361
  • 1
  • 5
  • 20
  • You do understand that ASP.NET code runs on the server, and that the server can't access your client's filesystem? – CodeCaster Jul 11 '19 at 08:30
  • OK , So how can I give access to Local Folder ? or any other ways to do this? – Biraz Dahal Jul 11 '19 at 08:43
  • What do you mean by "local folder" anyway? Also, your `downloadTextFileFromLocal()` method is a static void, it doesn't do anything. Do you intend to return a filename if something is found (**on the server**), and then return that file to the user? – CodeCaster Jul 11 '19 at 09:05
  • 3
    @BirazDahal start with explaining what you actually want to do. `Download from local` is self-contradictory. `Download **to** local` means downloading a file *from* the server to a local folder on the user's machine. That's easy, explained in the docs and multiple SO questions - use `return File(pathToTheServerFile)`. If you want to send a file to the server, you **upload it** – Panagiotis Kanavos Jul 11 '19 at 09:07
  • 3
    To *upload* a local file to the server, you use an [`input type='file'`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file) control in HTML, or execute a properly written POST request with the file's contents. Again, there are multiple SO questions that show how to do this – Panagiotis Kanavos Jul 11 '19 at 09:09
  • "the file doesn't downloads" because you don't do anything to make it download. You search for a filename and then...nothing. You just stop. Surely you can see that? A void method never returns anything. Values don't get returned by magic. You need to return the file. – ADyson Jul 11 '19 at 09:47

0 Answers0