📜 ⬆️ ⬇️

Using Backload for Downloadable Files in ASP.NET MVC

When I faced the task of organizing a beautiful and convenient mechanism for uploading files to a server with support for uploading large files, displaying download progress and other pleasures of the modern web, I began to search for what is available so as not to sculpt another bike.

With the Javascript aploaders, everything turned out to be very good - there are a lot of options, for all requirements and tastes. But with the server components for the implementation of the back end, the situation was somewhat worse. Most of the solutions I found were small examples of handlers that I wouldn’t need to file more than one file for real use.

As a result, for work on the client jQuery-File-Upload , which was repeatedly discussed on the habre, was chosen, the benefit for it is the angularjs wrapper (which was relevant), and its documentation found a link to the implementation of the backend, which I wanted to tell - Backload .

Backload is a complete backend implementation for several js-plugins for upload (including jquery-file-upload). The project is constantly evolving, has a decent set of settings and features and allows you to quickly raise an intelligently working mechanism for receiving downloadable files.
')
The project has several editions - free (standart) and a number of paid ones. But this fly in the ointment is compensated by the fact that for most of the tasks the free option is enough with my head (I had enough of it completely).

To get started, just install the Backload package from nuget or put it immediately with jquery-file-upload .
For the most simple purposes - a simple installation is enough; you just need to specify the necessary URL for jquery-file-upload:

var fileUploadUrl = "/Backload/UploadHandler"; $('#fileupload').fileupload({ url: fileUploadUrl }); 


And that's all. Requests will be processed by the embedded handler.

For situations where this is not enough and you want to customize and customize the process in more detail - you can customize the behavior of Backload by editing the configuration file (.config), or you can get into the work of the handler by processing its events.

To do this, you need to create your own controller, and specify jquery-file-upload to its URL. After that, in our controller, we create an instance of FileUploadHandler, subscribe to those of its events that we need and pass on to it processing.

An example of a controller with subscriptions to Backload events
 public class FileUploadController : Controller { public async Task<ActionResult> FileHandler() { FileUploadHandler handler = new FileUploadHandler(Request, this); handler.IncomingRequestStarted += handler_IncomingRequestStarted; handler.AuthorizeRequestStarted += handler_AuthorizeRequestStarted; handler.AuthorizeRequestFinished += handler_AuthorizeRequestFinished; handler.GetFilesRequestStarted += handler_GetFilesRequestStarted; handler.GetFilesRequestFinished += handler_GetFilesRequestFinished; handler.GetFilesRequestException += handler_GetFilesRequestException; handler.StoreFileRequestStartedAsync += handler_StoreFileRequestStartedAsync handler.StoreFileRequestFinished += handler_StoreFileRequestFinished; handler.StoreFileRequestException += handler_StoreFileRequestException; handler.DeleteFilesRequestStarted += handler_DeleteFilesRequestStarted; handler.DeleteFilesRequestFinishedAsync += handler_DeleteFilesRequestFinishedAsync; handler.DeleteFilesRequestException += handler_DeleteFilesRequestException; handler.OutgoingResponseCreated += handler_OutgoingResponseCreated; handler.ProcessPipelineExceptionOccured += handler_ProcessPipelineExceptionOccured; ActionResult result = await handler.HandleRequestAsync(); return result; } /*  */ } 


After this, it becomes possible to influence the processing of the file upload request.
For example, under some condition to change the file name, the path to save, or simply refuse to apload according to some criteria.

Simple example
 async Task handler_StoreFileRequestStartedAsync(object sender, StoreFileRequestEventArgs e) { var fileName = e.Param.FileStatusItem.FileName; if (fileName.Equals("some_bad_name.tmp", StringComparison.OrdinalIgnoreCase)) { fileName = "some_good_name.tmp"; e.Param.FileStatusItem.FileName = fileName; e.Param.FileStatusItem.UpdateStatus(true); } } 


Or, for example, at the end of the apload something to do with the file.

Another simple example
 void handler_StoreFileRequestFinished(object sender, StoreFileRequestEventArgs e) { var fileName = e.Param.FileStatusItem.FileName; var folder = e.Param.FileStatusItem.StorageInfo.FileDirectory; DoSomeOperations(folder, fileName); } 


According to the events there is a very detailed description in the documentation and even examples of their use . The spectrum of events is large enough to intervene in the process exactly when needed. In particular, it turned out to be very convenient for me to implement a rather complicated logic of determining the right path for uploading files, checking permissions for files into specific folders, my own logging of operations with files and adding metadata files to downloadable files.

I hope this article will be useful to someone and will show that there are quite simple, but convenient solutions for working with downloadable files.
Thanks for attention!

Source: https://habr.com/ru/post/204878/


All Articles