我正在与一个在页面中呈现多媒体内容的处理程序合作。
这个想法是,该处理程序访问文件并使用扩展名确定类型,然后将其呈现problem是大多数时候处理程序本身被下载并且不提供多媒体。
这是代码:
FileInfo file = new FileInfo(filePath);
byte[] bytes = new byte[file.Length];
using (FileStream fs = file.OpenRead())
{
fs.Read(bytes, 0, bytes.Length);
}
string extension = Path.GetExtension(filePath);
string mimeDeclaration;
if (".tif" == extension)
mimeDeclaration = "tiff";
string[] imagenes = new string[] {".jpg", ".jpeg", ".bmp", ".gif", ".png"};
if (imagenes.Any(x => x.Contains(extension)))
mimeDeclaration = extension.Substring(1);
else
mimeDeclaration = string.Empty;
context.Response.ClearContent();
context.Response.ClearHeaders();
context.Response.ContentType = "image/" + mimeDeclaration;
context.Response.BinaryWrite(bytes);
The filePath
变量有效。
您能否帮助我避免处理程序不呈现多媒体内容?
我想我现在明白了,当哑剧声明为空或错误则您无法下载图像。
这发生在您的代码中,因为图像的 mime 类型不是always“image/”加上文件扩展名:
context.Response.ContentType = "image/" + mimeDeclaration;
例如,对于 .jpg 图像,它是
图片/jpeg
否则可能是因为它是一个 tiff 图像,在这种情况下你的 else 子句正在设置mimeDeclaration
返回到一个空字符串。
提示:通过文件扩展名检测 MIME 类型并不理想,请在此处查看我的做法:Urlmon.dll 中 FindMimeFromData 方法的替代方法,该方法具有更多 MIME 类型 https://stackoverflow.com/questions/15300567/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)