2

I have been doing a project using web and ran into a problem. I need to download a file, but the file is automatically download on visit, so no actual URL is given.

I tried WebClient but i realized that i won't be able to do it that way. Also i have tried using WebBrowser but there i face another problem. The file is downloaded but,

1) There's a dialog about saving the file.

2) I don't know where the file is downloaded.

3) WebBrowser download event uses no special EventArgs

WebBrowser wb = new WebBrowser();
wb.Navigate("https://thunderstore.io/package/download/Raus/IonUtility/1.0.1/")

private void wb_FileDownload(object sender, EventArgs e)
{
    // The download code, but no download path
}

Any ideas how can i resolve this problem ?

AndrewToasterr
  • 459
  • 1
  • 5
  • 16
  • Take a look at https://stackoverflow.com/questions/3538874/suppressing-the-save-open-dialog-box-in-a-webbrowser-control - this might be a solution. – misticos Oct 05 '19 at 11:10

1 Answers1

3

Try this approach:

var client = new HttpClient
{
    BaseAddress = new Uri("https://thunderstore.io/")
};

var response = await client.GetStreamAsync("package/download/Raus/IonUtility/1.0.1/");

var fn = Path.GetTempFileName();

using (var file = File.OpenWrite(fn))
{
    await response.CopyToAsync(file);
}

At the end fn will hold the local file name. There is no dialog and you have the full control.

ZorgoZ
  • 2,974
  • 1
  • 12
  • 34
  • 1
    @SuPEr_Toaster_Official As you have not shared more of your application please read this to properly adopt the usage of `HttpClient` to your use-case: https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/ TL;DR; You can and you should reuse the client instance as long the base address is the same. If this is a one-shot case you should put `using` around it. – ZorgoZ Oct 05 '19 at 12:47