Request.Files
. We communicate with client scripts via JSON. For this, the return value in the controller will be JsonResult
, or its heirs. But the content type is text / html, otherwise browsers will offer to save the content like application / json.
- private readonly IFileService _fileService = new FileServiceStub ( ) ;
- /// <summary>
- /// Processing upload files to the server.
- /// </ summary>
- /// <returns> Json Result containing the list of file names. </ returns>
- [ HttpPost ]
- public JsonResult UploadFiles ( )
- {
- ICollection < string > collection = _fileService. Upload ( Request. Files ) ;
- return new JsonResult
- {
- ContentType = "text / html" ,
- Data = new { collection }
- } ;
- }
- < form id = "upload-files" action = "/ File / UploadFiles" method = "post" enctype = "multipart / form-data" >
- < div >
- Upload files:
- < input class = "multi-file-upload" type = "file" name = "file" / > < / div >
- < input type = "submit" value = "Download" > < / input >
- < / form >
- <! - Download message output location ->
- < div id = "messages" > < / div >
- < h3 > List of uploaded files < / h3 >
- < ol id = "file-list" > < / ol >
- // Executes the code when the document is fully loaded.
- $ ( document ) . ready ( function ( ) {
- var form = $ ( '# upload-files' ) ; // Download form.
- var messages = $ ( '#messages' ) ; // The notification area.
- var list = $ ( '# file-list' ) ; // List of files already uploaded.
- // Configure MultiFile.
- $ ( '.multi-file-upload' ) . MultiFile ( {
- max : 5 , // Number of files uploaded at once.
- STRING : { // String Translation. You can insert 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 , // Ship using iframe.
- dataType : "json" , // Exchange is conducted using json.
- // Perform before sending files.
- beforeSubmit : function ( ) {
- // Uncomment the next line, if submit is called from javascript.
- // $. fn.MultiFile.disableEmpty ();
- // Display a download message. It is better to tie jQuery.blockUI here, but I leave it for clarity.
- messages. html ( '<img src = "/ Content / busy.gif" /> Load file (s) ...' ) ;
- } ,
- // Execute 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 boot 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 ( '<li>' + this + '</ li>' ) ;
- } ) ;
- }
- } ,
- // Execute on error.
- error : function ( xhr , textStatus , errorThrown ) {
- // Clear the file upload form.
- $ ( '.multi-file-upload' ) . MultiFile ( 'reset' ) ;
- alert ( 'An error occurred while loading files:' + errorThrown ) ;
- }
- } ) ;
- } ) ;
- <! - File upload form. May be arbitrary. ->
- < div id = "file_upload_button" >
- <a href = "#"> Add < / a > a new file. < / div >
- < div id = "uploading" style = "display: none" > < img src = "/Content/busy.gif" alt = "Download" align = "left" / > Loading. < / div >
- <! - List of downloaded files ->
- < ol class = "files" > < / ol >
- $ ( document ) . ready ( function ( ) {
- new AjaxUpload ( $ ( '#file_upload_button' ) ,
- {
- // Whether to send the file immediately after selection
- autoSubmit : true
- // Where to send
- action : '/ File / UploadFiles' ,
- // Variable name to store the file.
- name : 'myfile' ,
- // Type of server response.
- response : 'json' ,
- // Fires before file upload
- // You can also return false to cancel.
- onSubmit : function ( file , extension ) {
- // Hide the file select button.
- $ ( '#file_upload_button' ) . hide ( ) ;
- // Show the download message.
- $ ( '#uploading' ) . show ( ) ;
- // If you need to load only one file:
- //this.disable ();
- } ,
- // Run after receiving a response from the server.
- // file is the name of the file specified by the client.
- // response - server response.
- onComplete : function ( file , response ) {
- $ ( '#file_upload_button' ) . show ( ) ;
- $ ( '#uploading' ) . hide ( ) ;
- // Get the javascript object.
- var resultObject = eval ( '(' + response + ')' ) ;
- // Add new uploaded files to the general list.
- if ( resultObject. collection . length > 0 ) {
- $. each ( resultObject. collection , function ( i ) {
- $ ( '.files' ) . append ( '<li>' + this + '</ li>' ) ;
- } ) ;
- }
- }
- } ) ;
- } ) ;
<input type="file"...
). Size: MultiFile (~ 20Kb) + jQuery.Form (~ 22Kb) = ~ 42Kb.Source: https://habr.com/ru/post/70249/
All Articles