Plupload Upload component + java implementation file upload example

Source: Internet
Author: User
Tags bind file upload save file silverlight


In order to be compatible with IE8 browsers, use Plupload: Support multiple file upload and upload progress, before the background of the program is not modified. Plugin Download Address: http://www.plupload.com/download/

Plupload is an interface-friendly file upload module on a Web browser that shows upload progress, automatic image thumbnails, and upload chunking. Multiple files can be uploaded at the same time.

As a result of the implementation of the file block upload, so you can meet in some limited upload size of the environment to upload large file requirements.

The code is as follows Copy Code

var uploader = new Plupload. Uploader ({
runtimes: ' Silverlight ',
Browse_button: ' pickfiles ',
Container: ' Container ',
Max_file_si Ze: ' 20mb ',
URL: "/" +context_name+ "/importfromexcelaction.uploadexcelfile.do",
Multipart:true,
Chunk_ Size: ' 10MB ',//split large file into
Unique_names:true,
Urlstream_upload:true,
Multiple_queues:false,
Filters : [{title: "Excel File", Extensions: "Xls,xlsx"}],
Silverlight_xap_url: '/' +context_name+ '/modules/common/attach/ Plupload/script/plupload.silverlight.xap '
});
Uploader.bind ("Init", function (up,params) {
$J (' #filelist '). HTML ("
Current Runtime:" + Params.runtime + " br> ");
});
Uploader.bind ("filesadded", function (up,files) {
$J. Each (Files, function (i, file) {
$J (' #filelist '). Append (
'
' +
File.name + ' (' + plupload.formatsize (file.size) + ') ' +
'
');
});
Up.refresh ();
});

$J ("#uploadfiles"). Click (function (e) {
Uploader.start ();
E.preventdefault ();
})

Uploader.bind (' uploadprogress ', function (up, file) {
$J (' # ' + file.id + "B"). HTML (file.percent + "%");
});

Uploader.bind (' Error ', function (up, err) {
$J (' #filelist '). Append ("
Error: "+ Err.code +
", Message:" + Err.message +
(Err.file?) ", File:" + err.file.name: "") +
"
"
);
Up.refresh (); Reposition Flash/silverlight
});

Uploader.bind (' fileuploaded ', function (up, file) {
$J (' # ' + file.id + "B"). HTML ("100%");
$J (' #filelist '). Append ("Upload success ...
");
$J (' #filelist '). Append ("Start working with Excel data ...").
");
var request = $J. Ajax ({
URL: '/' +context_name+ '/importfromexcelaction.importexcel.do ',
Type: "Post",
data:{
Template: $J ("#selectTemplate"). Val (),
FileName:file.name
}
});

Timing Get Import Status
var task = Window.setinterval ("Getimportstatus ()", 5000);

Request.done (function (data) {
$J (' #filelist '). Append ("Success: +data.success+", msg: "+data.msg+"
");
Window.clearinterval (Task);
});
Request.fail (function (JQXHR, textstatus) {
$J (' #filelist '). Append ("Request to process Excel data failed: +textstatus+"
");
Window.clearinterval (Task);
});


});

Uploader.init ();

In the background, you can append the contents of the file using the FileInputStream construction method. New FileOutputStream (Fullname,isappend)
Plupload uses the "Multipart/form-data" form to upload files, where each chunk makes a request, with two fields in the form, "chunk" and "chunks", where "chunk" Is the ordinal number of the file block that is currently being processed (counted from 0), and "chunks" is the total number of blocks of the file. Specific implementation:

The code is as follows Copy Code

/**
*
Uploading files using the Plupload component


* @param request
* @param response
* @param repath the relative path to save the file, Webroot as the root
* @return
*/
Publ IC static string Uploadfiles (HttpServletRequest request,httpservletresponse Response, String repath) {
String filename = null;
int chunk = 0;//The file block ordinal currently being processed
int chunks = 0;//Chunk Upload Total
Boolean ismultipart = SERVLETFILEUPLOAD.ISMULTIPARTC Ontent (Request);
//To determine if the current form is "Multipart/form-data"
if (ismultipart) {
Servletfileupload upload = new Servletfileupload (); //webroot Absolute path
String Webrootpath = Filehelper.getserverwebroot ();
try {
Fileitemiterator iter = upload.getitemiterator (request);
while (Iter.hasnext ()) {
Fileitemstream item = Iter.next ();
String name = Item.getfieldname ();
InputStream input = Item.openstream ();

if ("Chunk". Equals (name)) {
Chunk = integer.valueof (streams.asstring (input));
Continue
}
if ("chunks". Equals (name)) {
chunks = integer.valueof (streams.asstring (input));
Continue
}

Handle a multi-part MIME encoded file.
if (!item.isformfield ()) {
Filename
filename = Item.getname ();
Save file Directory Absolute path
File dir = new file (Webrootpath+repath);
if (!dir.isdirectory () | |!dir.exists ()) {
Dir.mkdir ();
}

Save File Absolute Path
String FullPath = webrootpath+repath+ "/" +filename;
if (chunk = = 0) {
File File = new file (FullPath);
if (file.exists ()) {
File.delete ();
}
Uploading files
Filehelper.uploadfile (input, fullpath);
}
if (Chunk > 0) {
Append files
Filehelper.uploadfile (input, FullPath, true);
}
if (chunk+1 = = Chunks | | chunks = 0) {
Break
}
}
}
}
catch (Exception e) {
Log.error (E, E.fillinstacktrace ());
E.printstacktrace ();
}
}

return filename;
}

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.