PrimeFaces 上传基于blueimp/jQuery-File-Upload。
当.serializeArray() 被调用时,该表单内的所有数据都将被序列化。
在这种情况下,您可以覆盖 add 选项的 PrimeFaces implementation 以为每个文件附加一个额外的输入文本。
所以结果应该是这样的:
现在这将是额外的一行代码,确切地说是here:
.append('<td class="title"><label>Title: <input name="title['+ file.name +']"></label></td>') //the only modification we have to do
额外的输入文本称为title[fileName],在这种情况下,您将通过当前文件名获取请求参数的值。
public void handleFileUpload(FileUploadEvent event) {
FacesContext context = FacesContext.getCurrentInstance();
Map map = context.getExternalContext().getRequestParameterMap();
String paramName = "title["+event.getFile().getFileName()+"]";
String fileWithTitle = (String) map.get(paramName);
}
这是add 选项的完整实现(假设您的widgetVar 是fileUpload)
$(document).ready(function() {
setTimeout(fileUpload, 1000);
})
function fileUpload() {
PF('fileUpload').jq.fileupload({
add: function(e, data) {
$this = PF('fileUpload');
$this.chooseButton.removeClass('ui-state-hover ui-state-focus');
if ($this.files.length === 0) {
$this.enableButton($this.uploadButton);
$this.enableButton($this.cancelButton);
}
if ($this.cfg.fileLimit && ($this.uploadedFileCount + $this.files.length + 1) > $this.cfg.fileLimit) {
$this.clearMessages();
$this.showMessage({
summary: $this.cfg.fileLimitMessage
});
return;
}
var file = data.files ? data.files[0] : null;
if (file) {
var validMsg = $this.validate(file);
if (validMsg) {
$this.showMessage({
summary: validMsg,
filename: file.name,
filesize: file.size
});
}
else {
$this.clearMessages();
//the only modification we have to do
var row = $('<tr></tr>').append('<td class="ui-fileupload-preview"></td>')
.append('<td>' + file.name + '</td>')
.append('<td class="title"><label>Title: <input name="title['+ file.name +']"></label></td>')
.append('<td>' + $this.formatSize(file.size) + '</td>')
.append('<td class="ui-fileupload-progress"></td>')
.append('<td><button class="ui-fileupload-cancel ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only"><span class="ui-button-icon-left ui-icon ui-icon ui-icon-close"></span><span class="ui-button-text">ui-button</span></button></td>')
.appendTo($this.filesTbody);
if ($this.isCanvasSupported() && window.File && window.FileReader && $this.IMAGE_TYPES.test(file.name)) {
var imageCanvas = $('<canvas></canvas')
.appendTo(row.children('td.ui-fileupload-preview')),
context = imageCanvas.get(0).getContext('2d'),
winURL = window.URL || window.webkitURL,
url = winURL.createObjectURL(file),
img = new Image();
img.onload = function() {
var imgWidth = null, imgHeight = null, scale = 1;
if ($this.cfg.previewWidth > this.width) {
imgWidth = this.width;
}
else {
imgWidth = $this.cfg.previewWidth;
scale = $this.cfg.previewWidth / this.width;
}
var imgHeight = parseInt(this.height * scale);
imageCanvas.attr({width: imgWidth, height: imgHeight});
context.drawImage(img, 0, 0, imgWidth, imgHeight);
}
img.src = url;
}
//progress
row.children('td.ui-fileupload-progress').append('<div class="ui-progressbar ui-widget ui-widget-content ui-corner-all" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0"><div class="ui-progressbar-value ui-widget-header ui-corner-left" style="display: none; width: 0%;"></div></div>');
file.row = row;
file.row.data('filedata', data);
$this.files.push(file);
if ($this.cfg.auto) {
$this.upload();
}
}
}
}});
}
只是将上面的代码包含在一些js文件中,并在</h:body>结束之前包含它
这是一个online Demo。
注意:如果用户选择了具有完全相同的名称和扩展名的多个文件,您可能会在此方法中遇到唯一的拖累。你会得到第一个标题两次!对于某些人来说这是可以的,因为用户不应该上传相同的文件。
这是在 PrimeFaces 5.0 和 Chrome 上进行的测试。