Posts

GZip with Pako

GZip is a super-fast compression technique, usable anywhere. In javascript, you can use the Pako library to compress your data. Browser <!DOCTYPE html> < html > < head > < title > Test Zip </ title > </ head > < body > < textarea id = "inputText" style = " width : 90 % ; height : 300 px ; " > </ textarea > < textarea id = "outputText" style = " width : 90 % ; height : 300 px ; " > </ textarea > < br > < input type = "button" value = "Go" onclick = "go();" > < script src = "https://cdn.jsdelivr.net/pako/1.0.5/pako.min.js" > </ script > < script > function go ( ) { console . log ( document . getElementById ( " inputText " ) . value . length ) ; var compressedBytes = p

Add EXIF date to photo

If you have a photo that you need to add a timestamp to the EXIF information, you can use the following .NET code. Apparently there are a ridiculous number of formats and standards which are incompatible with each other and conflicting, but from what I’ve found, this should cover regular use cases. I’m sure there’s a better solution, but this requires that you have some unrelated image file that has the appropriate tags in it, in case your photo is missing the properties entirely. Not exactly a great solution, but it does seem to work. static void Go ( string oldFile , string newFile ) { DateTime theDate = new DateTime ( 2009 , 1 , 15 ) ; Func < int , PropertyItem > getPropItem = id = > { using ( var image = Image . FromFile ( @" c:\temp\IMG_6139.jpg " ) ) { // Some unrelated image file that already has EXIF data return image . GetPropertyItem ( id ) ; } } ; using ( var image = Image . Fr

jQuery AJAX

I’m sure there’s a perfectly good explanation for why the “always” function has two different signatures – one for requests that were successful and a different one for unsuccessful ones. But whatever their reason, they’re wrong. This is just plain stupid that you don’t know what kind of object the first and third parameters are. But they never asked me before they implemented it, so there’s nothing I can do about it… var jqXHR = jQuery . ajax ( ajaxParams ) ; jqXHR . done ( function ( data , textStatus , jqXHR ) { } ) ; jqXHR . fail ( function ( jqXHR , textStatus , errorThrown ) { } ) ; jqXHR . always ( function ( data | jqXHR , textStatus , jqXHR | errorThrown ) { } ) ; The ajaxParams object contains lots of different options - the ones I generally use are as follows : { // absolute or relative url : " /path/to/some/url " , // will you be receiving "json", "html", "text", or "xml" back // 9

jQuery Filter by Data

jQuery won’t let you search using a regular selector for data attributes if those data attributes were added using the data function: jQuery ( " #something " ) . data ( " somekey " , " someval " ) ; jQuery ( " #something[data-somekey=someval] " ) ; // does not find it Instead, define a new function: jQuery . fn . filterByData = function ( prop , val ) { return this . filter ( function ( ) { return jQuery ( this ) . data ( prop ) == val ; } ) ; } ; jQuery ( " #something " ) . filterByData ( " somekey " , " someval " ) ; // finds it StackOverflow question: http://stackoverflow.com/questions/4191386/jquery-how-to-find-an-element-based-on-a-data-attribute-value Stackoverflow answer: http://stackoverflow.com/a/15651670/111266

jQuery noConflict

If you need to inject a specific version of jQuery into a webpage that may or may not already have a different version of jQuery, or if you specifically need two different versions loaded, then you can use jQuery.noConflict. You might use this if you’re building a custom control that gets injected into a page, where you want to make sure your stuff works as intended, but you don’t want to accidentally upgrade or downgrade the main page’s jQuery. This sample will show how to load jQuery 1.8.3 into a page which may or may not already have a different version loaded. var _jq183 ; function load183 ( ) { if ( typeof window . jQuery === " function " && window . jQuery . fn && window . jQuery . fn . jquery === " 1.8.3 " ) { _jq183 = window . jQuery ; return ; } if ( typeof window . jQuery === ' undefined ' || ! window . jQuery . fn || ( window . jQuery . fn . jquery !== " 1.8.3 "

JSONP with jQuery and ASP.NET

JSONP allows you to execute something similar to AJAX, across domains without worrying about whether the request is across domains, or any special CORS configuration. It’s not generally a huge deal to set up CORS, but JSONP is guaranteed to work in all browsers and servers without any special handling. function go ( ) { jQuery . ajax ( { // url: "/Home/DoSomething", // Regular MVC // url: "/Handler1.ashx", // WebForms data : { xyz : " World " } , dataType : " jsonp " , type : " get " } ) . done ( function ( result ) { console . log ( result ) ; } ) ; } // Regular MVC - no special filters, return types, etc. public ActionResult DoSomething ( string xyz ) { Response . ContentType = " application/json " ; var foo = new { FirstName = " John " , LastName = " Doe " , Message = " Hello " + xyz }

Is Development in Razor View

I find myself needing to know if I'm in development from inside a Razor view. In the C# code, you can generally use the #DEBUG preprocessor directive , but it’s not quite so easy in Razor. I've used an extension method on HtmlHelper to kind of cheat, but the “correct” way seems to be to inject the host environment and call the IsDevelopment method: @using Microsoft . Extensions . Hosting @inject Microsoft . AspNetCore . Hosting . IWebHostEnvironment HostEnvironment @if (HostEnvironment . IsDevelopment()) { < link rel = "stylesheet" href = "~/css/site.css" asp-append-version = "true" > } else { < link rel = "stylesheet" href = "~/css/site.min.css" asp-append-version = "true" /> } @if (HostEnvironment . IsDevelopment()) { < script src = "~/js/bundle.js" asp-append-version = "true" > </ script > } else { < script src = "~/js/bundle.mi