t:inputFileUpload不上传文件 [英] t:inputFileUpload not uploading the file

查看:299
本文介绍了t:inputFileUpload不上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用myfaces ExtensionFilter来上传一个文件,但是这个文件没有被设置到我的bean进行进一步的处理。



这是代码:

 < h:form id =uploadFileFormenctype =multipart / form-data> 
< tom:inputFileUpload id =file
value =#{paramUpload.uploadFile}>
< f:valueChangeListener type =com.bosch.de.plcd.plugin.ParamFileUpload/>
< / tom:inputFileUpload>
< a4j:commandButton value =#{tpMsgs.upload}
styleClass =buttonaction =#{paramUpload.uploadParamFile}
onclick =javascript:updateParentScreen() ;>
< / a4j:commandButton>
< / h:表格>

和web.xml配置如下

< pre class =lang-html prettyprint-override> < filter>
< filter-name>扩展程序过滤器< /过滤器名称>
< filter-class> org.apache.myfaces.webapp.filter.ExtensionsFilter< / filter-class>
< / filter>
< filter-mapping>
< filter-name>扩展程序过滤器< /过滤器名称>
< servlet-name> Faces Servlet< / servlet-name>
< / filter-mapping>

由于 UploadFile 未设置为bean,我也尝试使用ValueChangeListener,永远不会少,它不是为了豆:)

代码

  public class ParamFileUpload implements ValueChangeListener {
$ b $ private private UploadedFile uploadFile;
$ b public void uploadParamFile(){
if(uploadFile!= null){
LOGGER.info(File type:+ uploadFile.getContentType());
LOGGER.info(文件名:+ uploadFile.getName());
LOGGER.info(文件大小:+ uploadFile.getSize()+bytes);


$ b public UploadedFile getUploadFile(){
return uploadFile;


public void setUploadFile(UploadedFile uploadFile){
this.uploadFile =(UploadedFile)uploadFile;


public void processValueChange(ValueChangeEvent event)
throws AbortProcessingException {
this.uploadFile =(UploadedFile)event.getNewValue();






$ b

您是否看到其他可能丢失的配置在这里?

解决方案

目前的JSF和Tomahawk版本无法通过ajax上传文件。由JSF创建的Ajax请求不支持也不使用 multipart / form-data 。战斧不是基于ajax的组件库。
$ b

 < h:commandButton value =#{tpMsgs.upload}
styleClass =buttonaction =#{paramUpload.uploadParamFile}
onclick =javascript:updateParentScreen();>
< / h:commandButton>

支持使用ajax上传文件是预定用于新的< h:inputFile> 组件的即将到来的JSF 2.2。 b
$ b

或者,您可以使用RichFaces自己的< rich:fileUpload> 。根据所使用的RichFaces版本,它可以使用Flash或者下面的iframe hack实现异步文件上传。


I am using myfaces ExtensionFilter to upload a file, but the file is not getting set to my bean for further processing.

Here is the code :

<h:form id="uploadFileForm" enctype="multipart/form-data">
    <tom:inputFileUpload id="file" 
        value="#{paramUpload.uploadFile}">
        <f:valueChangeListener type="com.bosch.de.plcd.plugin.ParamFileUpload" />
    </tom:inputFileUpload>
    <a4j:commandButton value="#{tpMsgs.upload}"
        styleClass="button" action="#{paramUpload.uploadParamFile}"
        onclick="javascript:updateParentScreen();">
    </a4j:commandButton>
</h:form>

and web.xml configuration is as below

<filter>
    <filter-name>Extensions Filter</filter-name>
    <filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>Extensions Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

Since UploadFile was not set to bean, I also tried using ValueChangeListener, never the less, its not se to bean :)

Bean code

public class ParamFileUpload implements ValueChangeListener {

    private UploadedFile uploadFile;

    public void uploadParamFile() {
        if(uploadFile != null) {
            LOGGER.info("File type: " + uploadFile.getContentType());
            LOGGER.info("File name: " + uploadFile.getName());
            LOGGER.info("File size: " + uploadFile.getSize() + " bytes");
        }
    }

    public UploadedFile getUploadFile() {
        return uploadFile;
    }

    public void setUploadFile(UploadedFile uploadFile) {
        this.uploadFile = (UploadedFile)uploadFile;
    }

    public void processValueChange(ValueChangeEvent event)
        throws AbortProcessingException {
        this.uploadFile = (UploadedFile) event.getNewValue();
    }
}

Do you see any other configuration that I might be missing here?

解决方案

It is not possible to upload files by ajax with current JSF and Tomahawk version. Ajax requests as created by JSF do not support nor use multipart/form-data. Tomahawk is not an ajax based component library. Use a normal command button instead.

<h:commandButton value="#{tpMsgs.upload}"
    styleClass="button" action="#{paramUpload.uploadParamFile}"
    onclick="javascript:updateParentScreen();">
</h:commandButton>

Support for uploading files with ajax is scheduled for upcoming JSF 2.2 with new <h:inputFile> component.

Alternatively, you can use RichFaces' own <rich:fileUpload>. Depending on the RichFaces version used, it uses either Flash or an iframe hack under the covers to achieve asynchronous file uploading.

这篇关于t:inputFileUpload不上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆