1

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.

JL_Funk
  • 41
  • 9
  • You can try examining the first fews bytes of a file to determine if it's a image file, i.e., the first few bytes of a png may start with, new byte[] { 0x89, 0x50, 0x4e, 0x47, 0x0D, 0x0A, 0x1A, 0x0A } – Rickey Aug 24 '20 at 16:41
  • Have you looked at the header of the respons. There is a property called content-type i.e., content-type: image/png . You can generally figure it from there what type of image it is. IF it says something like octa then it is difficult to figure out type of image, – Umang Aug 28 '20 at 08:43

0 Answers0