-
Notifications
You must be signed in to change notification settings - Fork 158
Example 04
###Using your own controllers Sometimes you want to setup your own controller instead using the built in controller to receive incoming requests, before calling Backload. to handle the file storage. Common scenarios are: Process additional form fields, logging, access control, executing additional business logic, etc. This can be easily achieved in two ways: You can derive your controller from BackloadController or you can instatiate a FileUploadHandler object and call the HandleRequest method on it. Now you have full controll and you can also leverage the great functionality of the Backload. component.
####How to setup your own controller Method 1: Deriving from BackloadController
// Method 1: Derive your controller from BackloadController and call the base class public class FileUploadDerivedController : BackloadController { public override JsonResult UploadHandler() { // Call base class to handle the file upload JsonResult result = base.HandleRequest();
return result; } }
Method 2: Instantiate a FileUploadHandler object (For demo purposes only, we added a FileUploadedStarted event handler):
// Method 2: Instatiate a FileUploadHandler object in your common controller and call the HandleRequest() method public class FileUploadInstanceController : Controller { public JsonResult UploadHandler() { FileUploadHandler handler = new FileUploadHandler(Request); handler.FileUploadedStarted += FileUploadedStarted; JsonResult result = handler.HandleRequest();
return result; }void FileUploadedStarted(object sender, Backload.Eventing.UploadStartedEventArgs e) { // Example: Prevent files of type jpeg to be processed if (e.ContentType == "image/jpeg") { // Cancel processing of the incoming file e.CancelAction = true; e.CancelMessage = "Upload canceled, File type not accepted"; } } }
####Conclusion This example shows how to use your own controllers to receive a request, to some processing and then call the Backload. handler. To test it, switch the urls in the main.js file and set breakpoints in both controller methods. Note: As a demo only, we've registered an event handler in this example, in order to cancel file strorage, if the file type is inappropriate.
Code examples: Example04
Backload. (Standard version): Copyright 2013, Steffen Habermehl, License (Standard version): MIT license
Professional and Enterprise (source code) version are available under a commercial license.
Follow us on Twitter: @Backload_MVC