0

I want to download a file directly from internet without exposing the url to the user from blazor. The user could be able to select the download location but never see the download link. I have a razor page for this.

I used WebRequest for this. It downloads the file but I am not able to select the location to save file Please help?

    @using (var client = new WebClient())
    {
        client.DownloadFile("https://s3.amazonaws.com/media.eremedia.com/uploads/2012/08/24111405/stackoverflow-logo-700x467.png", "File.png");
    }
Alias Varghese
  • 2,104
  • 3
  • 24
  • 52
  • I'm assuming by "location" you mean "location on the server"? And it's unclear what the problem is really - what exactly do you mean by "not able to select the location"...I see no place in your code where you've asked for user input, or even tried to set the folder in any way - you've simply hard-coded a string containing the filename. If you put a full path in there e.g. `C:\\Folder1\Folder2\File.png`, it will download to that specific path. It's up to you if/how you ask the user for their opinion on the folder to choose. – ADyson Sep 17 '20 at 19:55
  • @ADyson I want to download it in client machine and I want to see it is downloading to my downloads folder as it do while downloading manually. I want to see the download starting while navigating to page – Alias Varghese Sep 17 '20 at 20:18
  • Ah I see. And what happens currently? You said it already downloads the file, so I assume there's no problem with that part. Your application can't choose the folder on the client machine though. The user controls that through their browser settings. Some browsers (like I.E. for example) can be configured to let the user choose each time where a newly downloaded file should be saved. Some just allow the user to configure a general Downloads folder where everything is sent. Either way though, your web application can't influence that setting - mostly for security reasons. – ADyson Sep 17 '20 at 20:29
  • @ADyson Currently it goes to my project location and it doesnt provide any progress of download started in any browser like we usually get for downloading – Alias Varghese Sep 17 '20 at 20:33
  • That's because you're downloading it to the server, not the client. If you want your server to act as a proxy in this way (which is basically what you're doing) you need to take the downloaded file and return it to the browser in a response to a user action (e.g. button click or something). N.B. you may need js interop to achieve it - https://stackoverflow.com/a/57394354/5947043 is one solution suggested. – ADyson Sep 17 '20 at 20:38
  • Or there's https://stackoverflow.com/questions/52683706/how-can-one-generate-and-save-a-file-client-side-using-blazor . Or even https://stackoverflow.com/questions/59596338/how-to-download-in-memory-file-from-blazor-server-side – ADyson Sep 17 '20 at 20:44
  • @ADyson do you have some sample for that? Is there a better way to do that without any user interaction? download the file directly on page load – Alias Varghese Sep 17 '20 at 20:44
  • See the various links I've posted above. You can't really do it on page load as such though, without involving some extra script. A HTTP request can either load a page, or download a file, it can't do both - the response to each request can only contain one thing at once. So you need a separate request to the server in order to get the file as well. Perhaps you could load the page, then immediately use JS (or C# perhaps, I don't know Blazor that well yet) to initiate another server-side method which returns the file. – ADyson Sep 17 '20 at 20:45

0 Answers0