GetValue "Content Type"! GetValue "Content Type". OpenSubKey ext ;. ToString ;. Generic; using System. ContainsKey mapping. Add mapping. Value, mapping. StartsWith ". TryGetValue extension, out mime? Generic ;. Linq ;. Value , mapping. Key ;. TryGetValue extension , out mime? TryGetValue mimeType , out extension. GetType "System. Instance BindingFlags. Static BindingFlags. For ASP. TryGetContentType fileName, out contentType ; vNext only Never tested, but looks like you can officially expand the mime types list via the exposed Mappings property.
NET Framework For. GetMimeMapping fileName ; If you need to add custom mappings you probably can use reflection to add mappings to the BCL MimeMapping class, it uses a custom dictionary that exposes this method, so you should invoke the following to add mappings never tested tho, but should prob. AddMapping string fileExtension, string mimeType. Improve this answer. Shimmy Weitzhandler Shimmy Weitzhandler My listing above has mappings. SamuelNeff You can of course add mappings using reflection MimeMapping.
They're more likely to change in the future since it's not an agreed upon contract and you'll get a runtime error instead of a compile-time error if that happens. If you are going to do this, you should at least add unit tests for it to be sure to catch it early when upgrading. NET versions. Besides that, if you're going to add mappings manually, you might as well maintain your own collection.
The class is nothing more than a wrapper on a dictionary. You just won't get errors anymore. We can disagree and argue, bottom line is you're right you can technically call it through reflection, but personally I would never recommend using reflection on private properties in a production application unless it was absolutely necessary and without proper safeguards to catch it early if it fails.
FWIW, System. Web has been removed from ASP. Show 8 more comments. StartsWith ". TryGetValue extension, out mime? Samuel Neff Samuel Neff Somehow I have a feeling that building an associative data structure from a data file at startup would be a bit saner. Hey pplz MSFT has now implemented it! Check this — Shimmy Weitzhandler.
Shimmy, interesting. JonPurdy, I finally got around to refactoring it to use a dictionary. AnthonyVO, funny, I originally had it as a switch. Actually when you create a switch on strings, it has more overhead than a dictionary. Decompile the code. What it actually does is it creates a dictionary of pointers and then looks up the pointer in the dictionary to jump to that location. Dictionary therefore actually has less overhead. Show 12 more comments.
GetExtension fileName. ToLower ; Microsoft. OpenSubKey ext ; if regKey! GetValue "Content Type"! GetValue "Content Type". Bryan Denny Bryan Denny Yes, this is bit dangerous since it comes from registry and can miss extensions from software not installed, example. In addition, this would be a very expensive call to perform a registry lookup every time you need a mime type.
Perhaps if it was cached, but I would still prefer the static dictionary method written by Samuel. Or if you're using Mono, you don't really have a Windows registry at your disposal GetMimeMapping fileName ; System. Community Bot 1 1 1 silver badge. Warlock Warlock 6, 8 8 gold badges 48 48 silver badges 73 73 bronze badges.
GetMimeMapping filenamewithext ; — Jay. Substring 1 ; switch extension. FabioG I'm not sure that's going to be all that useful to you. Barrie Could you do something like : simply use the first in the list, to get a standard extension a stab at lowest common denominator e.
Guessing this would be good for some standard types but fail for plenty in this v long list. Overhead can be avoided simply by making it a static. A switch-case with strings, however, is a horrible, horrible thing when it comes to 'overhead'. Simply speaking It is a magnificently elegant little operation.
It also doesn't work on strings, at all, meaning what you wrote is really just a bunch of string compares. A Dictionary object beats that any day. Nyerguds, I don't believe your assertion is correct. See: stackoverflow. Basically, compilers are smarter then you think.
Show 1 more comment. Thomas Levesque Thomas Levesque k 62 62 gold badges silver badges bronze badges. If you're on Windows, yes.
The basic idea is "find where this information is given," and import that into a data structure of some sort. OK, ASP. NET can be run on Linux thanks to Mono, but that's not a very common scenario, so I think it's quite safe to assume that acidzombie24 is using Windows I would assume Windows is a given when talking about ASP.
Be careful with this solution, as the result depends on the software the client has installed. Yes, Mark's right. Seems I need Adobe Acrobat installed for. So, seems the program associated with the extension maintains this mimetype not windows. Stacked 5, 6 6 gold badges 53 53 silver badges 71 71 bronze badges. Cymen Cymen Oh, that is pretty!
It's useful for checking downloads against the submitted content type and adding a default extension where needed. Nyerguds That sounds useful. If you want to do a PR to add it to the above, I'd be happy to merge it. Or you can do your own thing of course! But both directions does sound useful.
The main problem seems to be the 'default' part I'l probably have to hardcode a list of those anyway — Nyerguds. Abel Valdez Abel Valdez 2, 1 1 gold badge 13 13 silver badges 29 29 bronze badges. I write blogs about. Bursts of code to power through your day. Web Development articles, tutorials, and news. Sign in. Changhui Xu Follow. File Upload via Swagger We will go over examples about uploading a single file, uploading a list of files, and uploading a file in a FormData….
Written by Changhui Xu Follow. More From Medium. Android Guy. Microservice or Monolith, that is the question…. Mikel Lindsaar. Lucas Cardoso. Path to Google summer of code. Sukhad Anand.
0コメント