// Source file header information: // <Copyright file = "FileUpload. js"> // Copyright (c) 2014-2034 Kencery. All rights reserved. // Created by: kencery) // Creation Time: // </Copyright> ; (Function ($ ){ $. Fn. SalesMOUNDUpload = function (options ){ Var defaults = { SaveUrl :'', JqInput :'', MaxSize: 1024*1024*100, // 100 M FnRemove: '', // remove a file, parameter: File Name FnComplete: ''// each file succeeded. Parameter: Content returned by the server }; Var opt = $. extend (defaults, options ); Function getByteToM (val ){ If (isNaN (val) return val; Val = val/(1024*1024 ); Val = Math. round (val * 100)/100; Return val; } Return this. each (function (){ Var $ this = $ (this ); $ This. empty (); If (typeof FormData = 'undefine '){ Alert ('The browser version is too low and cannot be uploaded! '); Return; } // Header If ($ this. find ('thead'). length = 0 ){ Var $ thead = $ ('<thead> '); Var $ th_tr = $ ('<tr> '); $ Th_tr.append ('<th> file name </th> '); $ Th_tr.append ('<th> type </th> '); $ Th_tr.append ('<th> size </th> '); $ Th_tr.append ('<th> Status </th> '); $ Th_tr.append ('<th> operation </th> '); $ Th_tr.appendTo ($ thead ); $ This. append ($ thead ); } Opt. jqInput [0]. addEventListener ('change', function (e ){ Var file = this. files [0]; If (! File ){ Return; } If (file. size> opt. maxSize ){ Window. alert ('file exceeds Max '); Return; } Var fd = new FormData (); Var $ table = $ this; Fd. append ("uploadFile", file ); Var xhr = new XMLHttpRequest (); Xhr. open ('post', opt. saveUrl, true ); Xhr. upload. addEventListener ("progress", uploadProgress, false ); Xhr. addEventListener ("load", uploadComplete, false ); Xhr. addEventListener ("error", uploadFailed, false ); Xhr. addEventListener ("abort", uploadCanceled, false ); // Table content Var $ tr = $ ('<tr> '); $ Tr. append ('<td class = "upload_name">' + file. name + '</td> '); $ Tr. append ('<td class = "upload_type">' + file. type + '</td> '); $ Tr. append ('<td class = "upload_size">' + getByteToM (file. size) + 'M' + '</td> '); $ Tr. append ('<td class = "upload_status">' + 0 + '</td> '); $ Tr. append ('<td class = "upload_action"> <a href = "javascript: void (0 ); "> '+' cancel '+' </a> </td> '); $ Tr. find ('. upload_action A'). unbind ('click'). bind ('click', function (){ Xhr. abort (); }); $ Table. append ($ tr ); Function uploadProgress (evt ){ If (evt. lengthComputable ){ Var percentComplete = Math. round (evt. loaded * 100/evt. total ); Required tr.find('.upload_status'0000.html (Math. round (percentComplete) + '% '); } Else { Fig fig ('unable to compute '); } } Function uploadComplete (evt ){ If (evt.tar get. status = 200 ){ Export tr.find('.upload_status'finished .html ('finished '); $ Tr. find ('. upload_action a'hangzhou.html ('delete '); If (typeof opt. fnComplete = 'function '){ Opt.fnComplete(evt.tar get. response ); } $ Tr. find ('. upload_action'). unbind ('click'). bind ('click', removeFile ); } } Function uploadFailed (){ Fig fig ('<a href = "javascript: void (0);"> × </a> '); $ Tr. find ('. upload_status A'). unbind ('click'). bind ('click', function (){ $ Tr. remove (); }); $ Tr. find ('. upload_action a'hangzhou.html ('retried '); $ Tr. find ('. upload_action A'). unbind ('click'). bind ('click', function (){ Xhr. send (fd ); }); } Function uploadCanceled (){ $ Tr. remove (); } Function removeFile (){ $ Tr. remove (); If (typeof opt. fnRemove = 'function '){ Opt. fnRemove (file. name ); } } Xhr. send (fd ); }, False ); }); }; } (JQuery )); |