Springmvc+jquery file drag and drop upload, select upload

Source: Internet
Author: User

Recently done a simple file upload function, JSP version, follow-up will improve more features, but now can be used, the need for small partners, direct reference to the following file content directly copy to their own project is OK, the effect

FILEUPLOAD.CSS:

. fileupload_box {position:relative;width:100%;height:100%;border:3px dashed #E5E5E5; Text-align:center;z-index: 2000;cursor:pointer;margin:0 Auto;}. fileupload_message {position:absolute;font-size:15px;color: #999; Font-weight:normal;height:30px;top:20px;width: 100%;}. Odao_browser{position:absolute;width:50%;height:36px;line-height:36px;left:25%;bottom:10px;-moz-border-radius: 3px; /* Firefox  /-webkit-border-radius:3px,/* Safari and Chrome */  border-radius:3px,/* Opera 10.5+, and IE using IE-CSS3 Browser *  /background: #5c6bc0;  Color:white;  Cursor:pointer;}. Odao_browser input {position:absolute;top:0;width:100%;height:100%;opacity:. 0;filter:alpha (opacity= 0);d irection: Ltr;cursor:pointer;}

Odao_fileupload_plugin.js:

(function ($) {$.fn.odao_fileupload=function (setting) {var defaults = {Url:document.    URL, method: ' POST ', Extradata: {}, maxfilesize:0, maxfiles:0, allowedtypes: ' * ', extfilter:null, Datatype:null, FileName: ' File ', Onuploadsuccess:function (Filename,url) {}};setting = $.extend ({},defaults,settin g) var fn = {init:function () {var _this = this;//Drag file upload var $targetBox = document.getElementById ($ (_this). attr (' id ')); $        Targetbox.addeventlistener ("Drop", function (e) {//Cancel default browser drag effect E.preventdefault ();        var fileList = e.datatransfer.files;        Fn.upload.call (_this,filelist[0]); }, False);//Select File to upload $ (_this). Find (' input '). On (' Change ', function (e) {var file = E.target.files;var file = File.item (0); Fn.upload.call (_this,file);});},upload:function (file) {if (Fn.checktype (file)) Return;var _this = This;var FD = new       FormData ();       Fd.append (' Files ', file); $.ajax ({URL: $ctx + "/fileupload/xxxxx.do", type: ' POST ', succEss:function (data) {if (data.success) Setting.onUploadSuccess.call (_this,file.name,data);           }}, Error:function () {showreturnmsg (' upload failed ', ' error '); }, DATA:FD, Cache:false, Contenttype:false, processdata:false});},//allowed on The file type checktype:function (file) {if (setting.allowedtypes! = ' * ' && setting.allowedTypes.indexOf (File.type)        = =-1) {showreturnmsg (' file format error, can only upload ' +setting.allowedtypes+ ' format file ', ' Error ');        return true;  } return False}};return This.each (function (i,n) {Fn.init.call ($ (n));});};  $ (document). On (' DragEnter ', function (e) {e.stoppropagation (); E.preventdefault ();});  $ (document). On (' DragOver ', function (e) {e.stoppropagation (); E.preventdefault ();}); $ (document). On (' Drop ', function (e) {e.stoppropagation (); E.preventdefault ();}) (JQuery);

Jsp:

<div id= "Target_box" class= "Fileupload_box" >
<p class= "Fileupload_message" > Drag the picture here </p>
<p class= "Odao_choosefileupload odao_browser" > select File
<input type= "file" name= "Files" multiple= "multiple" >
</p>

Call Method:

$ (' #target_box '). Odao_fileupload ({      onuploadsuccess:function (filename,attachmenturl) {      ///Save the upload file information and record the log $. Ajax ({type: "post", url: ', data: {'}, DataType: "JSON", success:function (data) {  if (data.success) {    alert (' Upload success ');   } else{        alert (' upload failed ');}}  );       },       allowedtypes: ' Image/png '});

Spring Configuration:

<!--file Upload related--><bean id= "multipartresolver"  class= " Org.springframework.web.multipart.commons.CommonsMultipartResolver ">        <!--One of the properties available ; The maximum upload size in bytes 100m-->        

Fileupload.java:

@RequestMapping ("/uploadprojectbgpicture.do") public String uploadprojectbgpicture (@RequestParam multipartfile[] Files,httpservletrequest request,httpservletresponse response) throws ioexception{     specific operation content      

Springmvc+jquery file drag and drop upload, select upload

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.