We fasten ajax-loading of files in asp.net mvc, using jQuery plugins
In this article, we will figure out how to dynamically upload files to asp.net mvc using jQuery , its plugins, and such and such a mother. To do this, we will make a “proof of concept” and try to enjoy the process. =)
To implement the plan, there are a lot of options. I will focus on two plugins: Ajax upload and Multiple File Upload .
Everything is trivial on the server. We will receive files from
This plugin works with jQuery.Form .
To implement the plan, there are a lot of options. I will focus on two plugins: Ajax upload and Multiple File Upload .
Part One: Server Side
Everything is trivial on the server. We will receive files from
Request.Files. We communicate with client scripts through JSON. To do this, the return value in the controller will be JsonResult, or its descendants. But we leave the content type text / html, otherwise browsers will offer to save content like application / json.Simple Action:
- private readonly IFileService _fileService = new FileServiceStub ();
- ///
- /// Processing uploading files to the server.
- ///
- ///
Json Result containing a list of file names. - [HttpPost]
- public JsonResult UploadFiles ()
- {
- Icollection
collection = _fileService.Upload (Request.Files); - return new JsonResult
- {
- ContentType = "text / html",
- Data = new {collection}
- };
- }
Part Two: Multiple File Upload Plugin
This plugin works with jQuery.Form .
HTML form:
- File upload:
List of downloaded files
Plugin setup:
- // Executes code when the document is fully loaded.
- $ (document) .ready (function () {
- var form = $ ('# upload-files'); // Download form.
- var messages = $ ('# messages'); // Notification area.
- var list = $ ('# file-list'); // List of already downloaded files.
- // Customize MultiFile.
- $ ('. multi-file-upload'). MultiFile ({
- max: 5, // Number of files downloaded at a time.
- STRING: {// Line feed. You can embed html-code (pictures, for example).
- remove: 'Remove',
- selected: 'Selected: $ file',
- denied: 'Invalid extension: $ ext!',
- duplicate: 'This file is already selected: \ n $ file!'
- }
- });
- // Set up ajaxForm
- form.ajaxForm ({
- iframe: true, // Send using iframe.
- dataType: "json", // We exchange using json.
- // Run before sending files.
- beforeSubmit: function () {
- // Uncomment the next line if submit is called from javascript.
- // $. fn.MultiFile.disableEmpty ();
- // Display a download message. It’s better to screw jQuery.blockUI here, but I leave it for clarity.
- messages.html ('
Loading file (s) ... ');
- },
- // Run after receiving a response about success.
- success: function (result) {
- // Uncomment the next line if submit is called from javascript.
- // $. fn.MultiFile.reEnableEmpty ();
- // Clear the file upload form.
- $ ('. multi-file-upload'). MultiFile ('reset');
- // Clear the download message.
- messages.empty ();
- // Add new uploaded files to the general list of uploaded files.
- if (result.collection.length> 0) {
- $ .each (result.collection, function (i) {
- list.append ('
- '+ this +'
');- });
- }
- },
- // Run on error.
- error: function (xhr, textStatus, errorThrown) {
- // Clear the file upload form.
- $ ('. multi-file-upload'). MultiFile ('reset');
- alert ('An error occurred while downloading files:' + errorThrown);
- }
- });
- });