所以这与问题所发生的情况类似:FileWebRequest not returning what it should
我正在尝试使用URI / WebRequest下载文件,其想法是我们可以在FTP / Web / Local文件操作中使用该方法。
以下是代码:
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自己的文档说Resp.ContentType的ContentType始终是“binary \ octet-stream”(找到here)。问题是,这会导致以后出现问题,因为我们期待一个图像文件(我们尝试使用此方法加载的文件类型是GIF,TIFF,JPEG等)。我们该怎么办?
修改的
特定错误发生在:
//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));
}
因为我们一直在返回MIME类型的Application / Octet-Stream。
答案 0 :(得分:0)
我做了一些额外的研究......显然这是微软提出的一个硬编码漏洞。
Here is the MSDN Article描述FileWebResponse.GetContentType ALWAYS返回二进制/八位字节流,然后进一步解释为Application / Octet-Stream。