Handler (MIME) does not work for multimedia content

I work with a handler that presents multimedia content on a page.

The idea is that this handler accesses the file and determines the type using the extension and represents it, the problem is that most of the time the loader itself loads and the media does not display.

Here is the code:

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 variable is valid filePath.

Could you help to prevent the handler from presenting multimedia content?

+4
source share
1 answer

I think I get it now when mimeDeclaration is empty or WRONG, then you will not get the image loading.

, mime "image/" :

context.Response.ContentType = "image/" + mimeDeclaration;

, .jpg image it

/JPEG

, , , tiff-, else mimeDeclaration .

: MIME , , : FindMimeFromData Urlmon.dll, MIME-

+2

Source: https://habr.com/ru/post/1599314/


All Articles