Download a file from website, without specific file url

Viewed 143

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 ?

1 Answers

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.

Related