1

So this is similar to what happened with question: FileWebRequest not returning what it should

I am trying to download a file using a URI/WebRequest with the idea being that we can use the method across FTP/Web/Local file operations.

Here is the code:

var req = WebRequest.Create(uri);
var fileExt = Path.GetExtension(uri.AbsolutePath).ToUpper().Substring(1);

switch (fileExt)
{
    case "JPG":
    case "JPEG":
        req.ContentType = _mimeJpeg;
        break;

    case "PNG":
        req.ContentType = _mimePng;
        break;

    case "TIF":
    case "TIFF":
        req.ContentType = _mimeTiff;
        break;
}

req.Timeout = timeout;

//The GetResponse call actually makes the request
var resp = req.GetResponse();

//Check the content type of the response to make sure it is
//one of the formats we support
if (resp.ContentType != _mimeTiff)
{
    var contentType = resp.ContentType;
    resp.Close();
    throw new Exception(String.Format("The image at the URL you provided is in an unsupported format ({0}).  " + "Uploaded images must be in either JPEG, PNG, or TIFF formats.", contentType));
}

Microsoft's own documentation says that the ContentType of Resp.ContentType is always "binary\octet-stream" (found here). The problem is that this causes an issue later on because we're expecting an image file (the file types we'd be trying to load with this method are GIF, TIFF, JPEG, etc). What should we be doing?

EDIT

The specific error is occuring at:

//Check the content type of the response to make sure it is
//one of the formats we support
if (resp.ContentType != _mimeTiff)
{
    var contentType = resp.ContentType;
    resp.Close();
    throw new Exception(String.Format("The image at the URL you provided is in an unsupported format ({0}).  " + "Uploaded images must be in either JPEG, PNG, or TIFF formats.", contentType));
}

Because we are getting back a MIME type of Application/Octet-Stream all of the time.

Community
  • 1
  • 1
mlw4428
  • 510
  • 1
  • 6
  • 21

1 Answers1

0

I did some additional research...apparently this is a hard-coded flaw Microsoft is putting in.

Here is the MSDN Article describing that FileWebResponse.GetContentType ALWAYS returns binary/octet-stream which is then interpreted further to Application/Octet-Stream.

mlw4428
  • 510
  • 1
  • 6
  • 21