Ajax+multipartfile uploading files to a local

Source: Internet
Author: User

. html

<th data-options= "field: ' Op ', align: ' center ', Width:220,formatter:rmtmanager.supportopformmater ()" > Operation </ Th>
<div id= "Uploadcertificate" >
<div class= "Easyui-layout" data-options= "Fit:true" >
<div data-options= "region: ' Center '" style= "padding:10px;" >
<div style= "margin-bottom:20px" >
<form id= "Excelform" method= "post" enctype= "Multipart/form-data" >
<div> Please select the certificate to upload:</div>
<input name= "Certificate"Class=" Easyui-filebox "data-options=" required: ' true ', ButtonText: ' Select File ' "style=" width:100% ">
<input type= "hidden" id= "AccountId" name= "accountId" value= ""/>
</form>
</div>
</div>
<div data-options= "region: ' South ', Border:false" style= "text-align:right;padding:5px 0 0;" >
<a id= "Import" href= "#" class= "Easyui-linkbutton" data-options= "iconcls: ' Icon-ok '" > Uploads </a>
<a id= "Cancel" href= "#" class= "Easyui-linkbutton" data-options= "iconcls: ' Icon-cancel '" > Cancel </a>
</div>
</div>
</div>


. js

(function ($) {
var pagetype= $ (' #glPageType '). Val ();
var ctx= $ (' #ctx '). Val ();
var editindex = undefined;
var busmng = Rmtcmhelper.createbusmnginst ({
busmngoptions:{
 toupload:function (flag) {
$ (' #accountId '). val (flag);
$ (' #uploadCertificate '). Window ({
width:300,
height:150,
Title: ' Certificate import ',
Minimizable:false,
Maximizable:false,
Collapsible:fals E,
Modal:true
});
$ (' #uploadCertificate '). Window (' open ');
},
 supportopformmater:function () {
return function (value,row,index) {
Re Turn ' <a href= "javascript:void (0);" class= "Todo" onclick= "Rmtmanager.totoken (' +row.id+ ')" > View token</a> &nbsp;&nbsp;&nbsp;&nbsp; '
+ ' <a href= "javascript:void (0);" class= "Todo" onclick= "rmtmanager.addconfig (' +row.id+ ')" > Configuration </A&G t;&nbsp;&nbsp;&nbsp;&nbsp; '
+ ' <a href= "javascript:void (0);" class= "Todo" onclick= "Rmtmanager.toview (' +row.id+ ')" > View </a>& amp;nbsp;&nbsp;&nbsp;&nbsp; '
+ ' <a href= ' javascript:void (0); "class=" Todo "onclick=" rmtmanager.toupload (' +row.id+ ') > on Certificate </a>&nbsp;&nbsp;&nbsp;&nbsp; ';
}
}
}); 
$ (' #import '). Bind (' click ', Function () {


$ (' #excelform '). Form (' Submit ', {
url:ctx+ "/wxb Ase/account/upload.html ",
Onsubmit:function () {
if ($ (' #excelform '). Form (' Validate '))
Rmthelper.showmask ();
},
Success:function (data) {
var data = eval (' (' + data + ') ');
if (data.success) {
Rmthelper.hidemask ();
$ (' #uploadCertificate '). Window (' close ');
Rmthelper.showmsg (data.content);
Rmthelper.getcurcurdist (). Reloadgrid ();
}else{
Rmthelper.hidemask ();
Rmthelper.showmsg (data.content);
}
}
});
});
$ (' #cancel '). Bind (' click ', Function () {
$ (' #uploadCertificate '). Window (' close ');
});
Controller


/**
* Upload Certificate
* @param request
* @return
*/
@RequestMapping (value= "Upload", method = Requestmethod.post)
@ResponseBody
Public Resultjsoninfo uploadcertificate (@RequestParam ("Certificate") MultipartfileCertificate, HttpServletRequest request) throws Exception {
String accountId = Request.getparameter ("AccountId");
String filename= "";
try {
String Picpath = "d://" + accountId + "/";
Determine if a certificate existed before
File Certificatefile = new file (picpath+accountid+ ". cer");

if (certificatefile.exists ()) {
The existing certificate is deleted
Certificatefile.delete ();
}
Create a catalog file based on a real path
File Picsavefile = new file (Picpath);
Determine if a path exists
if (!picsavefile.exists ()) {
Create a path if it does not exist
Picsavefile.mkdirs ();
}
if (!certificate.isempty ()) {
try {
byte[] buffer =new byte[1024*1024];
int bytesum = 0;
int byteread = 0;
Filename=accountid+ ". cer";
FileOutputStream fs=new FileOutputStream (Picpath + fileName);
Bytesum+=byteread;
Fs.write (Buffer,0,byteread);
System.out.println ("File Stream" +certificate.getoriginalfilename ());
Dump file */
/*certificate.transferto (New File (Picpath)); */
} catch (Exception e) {
E.printstacktrace ();
}
return new Resultjsoninfo (true, "upload succeeded" + ",");
}
}catch (Exception e) {
E.printstacktrace ();
return new Resultjsoninfo (false, "upload failed" + ",");
}
/*logger.info ("Import successful number of strokes:" +count+ ", Time:" + (End-start) + "milliseconds"); */
return new Resultjsoninfo (true, "upload succeeded" + ",");
}

The purple ones have to be named the same.

Ajax+multipartfile uploading files to a local

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.