Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get the file extension or type from a file loaded into MemoryStream?

I am creating a service in C# that is to be used by an ASP.Net Web API that will process and store files.

As part of the Web API I take the file(s) and store them in MemoryStreams which are then passed through to a service for actual processing and storage.

Part of the service will be to generate a new filename for each of the files.

so that leads me to the question, is it possible to find out what file type/extension the file was now that it's in a MemoryStream?

like image 585
Darren Wainwright Avatar asked Jul 17 '14 16:07

Darren Wainwright


1 Answers

No for extension, you can guess type:

Unless content of the file somehow contains name/extension MemoryStream by itself does not contain any information about original file name.

You may try to detect type of the file (many formats have magic number/signature in the beginning for example) by reading some/all content of the stream.

There are also several similar questions like Using .NET, how can you find the mime type of a file based on the file signature not the extension (discusses usage of UrlMon to detect type).

like image 186
Alexei Levenkov Avatar answered Sep 23 '22 05:09

Alexei Levenkov