Подтвердить что ты не робот

Загрузка файлов с интерфейсом не работает?

Попытка получить простую загрузку файлов, и все, что я получаю, это зависающая строка состояния AJAX и ее. Мои резервные bean выходы отображают правильное имя при подготовке и загрузке.

Я делаю это неправильно? оба выхода кажутся мне правильными.

JSF 2.0 Прайвы 3.4

        <h:form>
            <p:commandButton id="downloadLink" value="Download" actionListener="#{filemanagement.prepDownload}"> 
                <p:fileDownload value="#{filemanagement.download}" />
            </p:commandButton>
        </h:form>

Поддержка bean:

private DefaultStreamedContent download;

public void setDownload(DefaultStreamedContent download) {
    this.download = download;
}

public DefaultStreamedContent getDownload() throws Exception {
    System.out.println("GET = " + download.getName());
    return download;
}

public void prepDownload() throws Exception {
    File file = new File("C:\\file.csv");
    InputStream input = new FileInputStream(file);
    ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
    setDownload(new DefaultStreamedContent(input, externalContext.getMimeType(file.getName()), file.getName()));
    System.out.println("PREP = " + download.getName());
}
4b9b3361

Ответ 1

См. Документация по интерфейсам 6.1

Если вам нравится использовать команду PrimeFaces commandButton и commandLink, отключите параметр ajax как fileDownload требует полного обновления страницы для представления файла.

<h:form>
  <p:commandButton id="downloadLink" value="Download" ajax="false" actionListener="#{filemanagement.prepDownload}">
    <p:fileDownload value="#{filemanagement.download}" />
  </p:commandButton>
</h:form>

Ответ 2

Внутри командной кнопки установите ajax = false и не используйте action или action listener для командной строки.

<h:form>
  <p:commandButton id="downloadLink" value="Download" ajax="false">
    <p:fileDownload value="#{filemanagement.prepDownload}" />
  </p:commandButton>
</h:form>

Bean:

public StreamedContent prepDownload() throws Exception {
    StreamedContent download=new DefaultStreamedContent();
    File file = new File("C:\\file.csv");
    InputStream input = new FileInputStream(file);
    ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
    download = new DefaultStreamedContent(input, externalContext.getMimeType(file.getName()), file.getName()));
    System.out.println("PREP = " + download.getName());
    return download;
}