I've got a download link like this:
https://someURL.com/PiPki.aspx?ident=594907&jezik=de
The download result could be a file with any file type. For example Picture.jpg
or something.pdf
.
How is it possible to download whatever file is behind this link with its original name and extension?
Via HTTP there is not only the possibility to transmit payload data, but there is also a header you can use to transmit meta-data. On the receiver side you can use that data e.g. to determine the name to store the file as.
In order to determine the file type, the HTTP response must have the correct Content-Type
header (see here). If the transmitted file is a PDF, the HTTP response would have the header field
Content-Type: application/pdf
Furthermore there is the possibility to pass a file name in the Content-Disposition
header (see here) if the disposition is set to attachment
(i.e. a downloadable file instead of inline content)
Content-Disposition: attachment; filename="something.pdf"
If there is a known Content-Type
, but no file name, your option would be to use a default file name and the extension matching the Content-Type
, for example download.pdf
. If the Content-Type
is missing or generic, you're having bad luck. You could try to go for the contents of the file, but this may or may not be successful and could be unreliable for certain file types.
Since this is a C# question
var client = new HttpClient();
using (var response = await client.GetAsync("https://someURL.com/PiPki.aspx?ident=594907&jezik=de"))
{
string fileName = null;
if (response.Headers.Contains("Content-Disposition"))
{
fileName = GetFileNameFromContentDisposition(response.Headers);
}
if (fileName == null && response.Headers.Contains("Content-Type"))
{
var extension = GetExtensionFromContentType(response.Headers);
fileName = $"download.{extension}";
}
using (var fileStream = File.OpenWrite(fileName))
{
await response.Content.CopyToAsync(fileStream);
}
}