Background: the front-end File Upload control used in the project is uploadify. After a while, everything went well. "Good news" came from the site. The customer wanted to use the iPad and upload images. Once the customer is familiar, R & D will have to work overtime. We know that uploadify depends on flash, so it won't work on iOS or MAC systems. Therefore, after some Google operations, we found plupload. Easy to use.
Page HTML code:
The head tag contains necessary JS files.
<script type="text/javascript" src="js/jquery-1.9.1.min.js" ></script><!-- art dialog --><script type="text/javascript" src="artdialog/artDialog.source.js?skin=blue"></script><!-- plupload --><script type="text/javascript" src="plupload/plupload.full.js"></script>
Page elements in the Body Tag
<! -- Trigger dialog box --> <a id = "uploadbtn" class = "optionbtn inline" href = "#"> File Upload </a> <Div id = "uploadcontent" class = "" style = "display: none; Height: 300px; overflow-X: hidden; overflow-Y: auto; "> <Div id =" choosefile "> <span> A single file can be smaller than 100 MB </span> <br/> <a id =" uploadify "href =" javascript: void (0); "> select file </a> </div> <Div id =" uploadfilequeue "style =" border: 1px solid # a7c5e2; Height: 228px; width: 350px; "> </div>
<pre id="console"></pre>
Code in the script tag
VaR globaluploader; function _ plupload () {var Uploader = new plupload. uploader ({runtimes: 'html5, Flash, Silverlight, html4', browse_button: 'uploadify ', // The ID attribute container: 'uploadcontent' of the DOM object viewed on the page ', // Div URL containing browse_button: '/uploadaction! Localupload. action ', // The Action flash_swf_url:'/folder/JS/plupload/moxie.swf 'for receiving file uploads, and silverlight_xap_url:'/folder/JS/plupload/moxie. xap ', filters :{?? Max_file_size: '100mb ', // restrict the size of the uploaded file mime_types: [// restrict the upload file type {Title: "image files", extensions: "JPG, GIF, PNG "}]}, init: {postinit: function () {response ('{uploadfilequeue'{.html ('') ;}, uploadfile: function (up, file) {// triggered after beforeupload }, beforeupload: function (up, file) {// triggered before upload after clicking the button. You can query files of the same name and check the remaining space. // check whether duplicate files exist, if yes, add parentheses and numbers at the end of the file name to differentiate $. ajax ({URL: "/folder/personal/personalaction! Getnewfilename. action ", type:" Post ", async: false, data: {'upfilename': file. name, 'globalpid ': globalpid}, datatype: "JSON", success: function (data) {// set the parameter up before uploading. setoption ('multipart _ Params ', {upfilename: data. newfilename, upfiletype: file. name. split (". ") [file. name. split (". "). length-1], // suffix upfilesize: file. size, parentid: globalpid}) ;}, error: function (XMLHttpRequest, textstatus, errorthro Wn) {messagealert ("sorry, file [" + file. name + "] failed upload Preparation", ''); // $ ('# uploadify '). uploadify ('cancel', file. ID ); // cancel an upload task by ID }});},???????????????????????? Filefiltered: function (up, file) {// triggered after selecting a file}, filesadded: function (up, files) {// Add the file to the queue var I = 0; // record the file list number and delete it with plupload. each (files, function (File) {folder ('{uploadfilequeue'{.html('{uploadfilequeue'}.html () + '<Div id = "' + file. ID + '"class =" uploadify-queue-item "> <Div class =" cancel "> <a href =" javascript: _ plupload_removefile (' + I + ', '+ file. ID + ') "> </a> </div> <SPAN class =" FILENAME ">' + file. name + '(' + plupload. formatsize (file. size) + ') </span> <B> </B> <Div class = "uploadify-Progress"> <Div class = "uploadify-progress-bar"> </div> </Div> </div> '); I ++;}) ;}, uploadprogress: function (up, file) {// triggered after you click Start upload. You can add a progress bar here and use file. percent document. getelementbyid (file. ID ). getelementsbytagname ('B') [0]. innerhtml = '<span>-' + file. percent + "% </span>"; // percentage $ ('#' + file.id).find('.uploadify-progress-bar').css ('width', file. percent + '%'); // progress bar}, error: function (up, err) {// document triggered by an error. getelementbyid ('console '). innerhtml + = "\ n Error #" + err. code + ":" + err. message;}, fileuploaded: function (up, file, Info) {// triggered when a file is uploaded // fires when a file is successfully uploaded. data = eval ("(" + info. response + ")"); // The data returned by the server. You can modify the file list on the page.}, uploadcomplete: function (up, files) {// triggered when all files are uploaded // fires when all files in a queue are uploaded .}}}); uploader. init (); globaluploader = Uploader;} function popupdialog () {artdialog ({ID: 'file-upload', Title: 'file upload', fixed: True, lock: True, content: $ ("# uploadcontent") [0], button: [{Name: 'start upload', focus: True, callback: function () {globaluploader. start (); Return false ;},{ name: 'close', callback: function () {response ('{uploadfilequeue'}.html (''); // Delete the content of the upload queue globaluploader. files. splice (0, globaluploader. files. length); // clear the content in the upload queue return true ;}}], close: function () {response ('{uploadfilequeue'{.html (''); // Delete the content of the upload queue globaluploader. files. splice (0, globaluploader. files. length); // clear the content in the upload queue}) ;}$ (function () {$ ('# uploadbtn '). click (function () {popupdialog () ;}); _ plupload ();});
The background code will not be written. I used the struts2 background action to use the private file to receive the file, and changed the other name to null. Currently, I do not know how to set this value in the control.
Then the effect is like this.
You need to add these CSS styles to the progress bar, that is, existing control.
<style type="text/css"> #uploadfileQueue { position: relative; left: 0; top: 0; border: 1px solid #a7c5e2; margin-bottom: 5px; height: 228px; width: 350px; overflow-x: hidden; overflow-y: auto; } .uploadify-queue-item { /* background-color: #F5F5F5; */ background-color: #FFF; -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px; font: 11px Verdana, Geneva, sans-serif; /* margin-top: 5px; */ margin: 5px 5px 5px 5px; max-width: 350px; padding: 10px; } .uploadify-progress { background-color: #E5E5E5; margin-top: 10px; width: 100%; } .uploadify-progress-bar { background-color: #0099FF; height: 3px; width: 1px; }</style>
The final effect. What else do you want uploadify to delete files in the queue?
Add this section to the style.
.uploadify-queue-item .cancel a { background: url(‘js/uploadify-cancel.png‘) 0 0 no-repeat; float: right; height: 16px; text-indent: -9999px; width: 16px;}
Of course, you must add the deleted JS Code. The official API contains removefile (file). However, it is not easy to use it here. So another method splice (Num, length) is used. Num is deleted from the first few, and length is the number of deleted.
Function _ plupload_removefile (removenum, fileid) {globaluploader. Files. splice (removenum, 1); // delete some files $ (fileid). fadeout ();}
Final effect.