Posts

Showing posts with the label Json

Uploading files asynchronously in asp.net mvc site

Hi, I've been looking for quite some time for a complete solution to uploading files in ajax for my site. For those of you who don't know, browsers won't let you upload files without making a full page reload. I haven't been able to find a good, working and cross browser solution for this problem. So after much research and development I have finally managed to solve this problem. Lets take a look at the following controller. public class HomeController : Controller { public ActionResult Index() { return View(); } [HttpPost] public ActionResult UploadImage(HttpPostedFileBase image) { try { if (Request.ContentLength > 1024 * 1024 * 2) { return UploadCompanyLogoResult(new { success = false, error = "File size too big. Maximum file size is 2MB." }); } if (Request.Files.Count == 0) { return UploadCompanyLog...

Json To Dictionary generic model binder

Hey all, Few days ago I was challenged by a good friend and colleague to write a generic model binder for a dynamic json. Why does he need such a thing ? I'll explain: Normally, when you write a classic website using any platform, the contract between the client and the server is obvious and well known. However, if you write a complex web system, and you have the need to build your client model dynamically without creating a massive model on the server that handles all kinds of properties and values, you might be interested in getting a generic dictionary that contains all your client values. To implement it I used a state machine to parse the json: public class DictionaryModelBinder : DefaultModelBinder { private const string _dateTimeFormat = "dd/MM/yyyy HH:mm:ss"; private enum StateMachine { NewSection, Key, Delimiter, Value, ValueArray } public override object BindModel(ControllerContext controllerC...