I have the following Situation: I am writing a piece of code that downloads images from a website to my files. The problem is that the links do not contain any information about the image type (could be .png, .jpg, or other). The images come from links that look like this (i changed all identifiable bits that could compromise my websites anonymity): https://mywebsite.com/path/components/path/path.product.file/download.php?auth=&productId=number&fieldName=path&dynamic=Y&fileId=number I get this link from an API so I cannot use a different link to the same picture. I already tried these:
How to download image from url (top two answers)
-
var uriWithoutQuery = uri.GetLeftPart(UriPartial.Path); var fileExtension = Path.GetExtension(uriWithoutQuery); String path = Path.Combine(directoryPath, $"{fileName}{fileExtension}"); Directory.CreateDirectory(directoryPath); ImagePath = path.ToString(); var imageBytes = await Client.GetByteArrayAsync(uri); await File.WriteAllBytesAsync(path, imageBytes);
which I got from here https://gist.github.com/MarcusOtter/b9b4ee3fc7be04469fd20480daa86c38
and a few more which I can't seem to find anymore.
I get the feeling that I am missing something or have done something wrong. If you can help me I would appreciate it very much :) Thank you in advance
EDIT: I figured out that it all didn't work because the link (if you are not logged in) sends you to an authentication window, from where you obviously can't download an image. Thank you for all the answers though.