Home > Web App > [JSF, Primefaces] Upload file with Plupload

[JSF, Primefaces] Upload file with Plupload

Step 1. Create a JSF, Primeface project with name “attachments

See my post: JSF + Primeface HelloWorld Example

Step 2. Folder structure

plupload

Step 3. index.xhtml


<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">

<h:head>
<title>Upload Attachment</title>
</h:head>
<h:body>
<h3>Upload File Example</h3>
<div id="container">
<div id="filelist">No runtime found.</div>
<br />
<a id="pickfiles" href="#">[Select files]</a>
<a id="uploadfiles" href="#">[Upload files]</a>
</div>
<hr/>
<h:form>
<strong>File Name: </strong>
<h:outputText id="filename" value="#{uploadBean.filename}"></h:outputText>
<p:remoteCommand name="updateAtt"
actionListener="#{uploadBean.uploadAttachment}" update="filename"
process="@this" />
</h:form>

<!-- Third party script for BrowserPlus runtime (Google Gears included in Gears runtime now) -->
<script type="text/javascript" src="http://bp.yahooapis.com/2.4.21/browserplus-min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<!-- Load plupload and all it's runtimes and finally the jQuery queue widget -->
<script type="text/javascript" src="plupload/plupload.full.js"></script>
<script type="text/javascript" src="home.js"></script>
<script type="text/javascript">
$(document).ready(function() {
uploadAttachment();
});
</script>
</h:body>
</html>

Step 4. home.js


function uploadAttachment(){
if(uploader) {
uploader.destroy();
}
var uploader= new plupload.Uploader({
runtimes : 'html5, flash',
browse_button: 'pickfiles',
url: '/attachments/index.xhtml'
});

uploader.bind('Init', function(up, params) {
$('#filelist').html("<div>Current runtime: " + params.runtime + "</div>");
});

$('#uploadfiles').click(function(e) {
uploader.start();
e.preventDefault();
});

uploader.init();

uploader.bind('FilesAdded', function(up, files) {
$.each(files, function(i, file) {
$('#filelist').append(
'<div id="' + file.id + '">' +
file.name + ' (' + plupload.formatSize(file.size) + ') <b></b>' +
'</div>');
});

up.refresh(); // Reposition Flash/Silverlight
});

uploader.bind('UploadProgress', function(up, file) {
$('#' + file.id + " b").html(file.percent + "%");
});

uploader.bind('Error', function(up, err) {
$('#filelist').append("<div>Error: " + err.code +
", Message: " + err.message +
(err.file ? ", File: " + err.file.name : "") +
"</div>"
);

up.refresh(); // Reposition Flash/Silverlight
});

uploader.bind('FileUploaded', function(up, file) {
updateAtt([{ name : 'filename', value : file.name }]);
$('#' + file.id + " b").html("100%");
});
}

Step 5. UploadBean


package ant.com;

import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;

@ManagedBean(name = "uploadBean")
public class UploadBean {
private String filename;

public String getFilename() {
return filename;
}

public void setFilename(String filename) {
this.filename = filename;
}

public void uploadAttachment(){
String filename = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("filename");
setFilename(filename);
}
}

References:

[1]. http://www.plupload.com/example_custom.php

Download source: https://drive.google.com/file/d/0B7JQL2BurAL4UnpyN1oydnNuNlE/edit?usp=sharing

Advertisements
Categories: Web App
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: