File ulpoad (docx, excel, pdf etc.)

Hi, In Cuba there was something like FileDescriptor to upload or download files. In Jmix I can’t find it… what is the best way to upload for example pdf file to database and then download it by clicking specified button? In Component Pallete is something like FileUploadField and it worked for .jpg image but does it also work for other file extensions?

What is my goal: Saving specified user’s documents to a list (

User:

@OneToMany(mappedBy = “userDocument”)
private List[Document] documents;

Document:

@JoinColumn(name = “USER_DOCUMENT_ID”)
@ManyToOne(fetch = FetchType.LAZY)
private User userDocument;

) and then be able to download chosen document.

I will be glad for help ^^ Have a nice day!

Hi,
Working with files is explained in a separate section of Jmix documentation: Managing Files :: Jmix Documentation.

If it remains unclear after reading the docs, please let us know. We will be happy to clarify unclear moments (and also will know what to improve in the docs).

Yes its working as I wanted to :wink: But DownloadFormat class doesn’t support PPTX. Can I add it somehow or it will be added in next updates?

Note that DownloadFormat isn’t an enumeration.
You can create any format in your code and pass it to the Downloader#download() method.

DownloadFormat pptx = new DownloadFormat("pptx-mime-type", "pptx");

Okay, that sounds fine. And what about adding new DownloadFormat to the getByExtension() method? Here is an example what I did:

@Subscribe(“fileField”)
public void onFileFieldFileUploadSucceed(SingleFileUploadField.FileUploadSucceedEvent event) {
fileField.setUploadButtonIcon(“font-icon:CHECK”);
fileField.setUploadButtonCaption(“File uploaded successfully!”);
String extension = “”;
String fileName = “”;
int i = event.getFileName().lastIndexOf(’.’);
if (i > 0) {
extension = event.getFileName().substring(i+1);
fileName = event.getFileName().substring(0, i);
}
getEditedEntity().setName(fileName);
getEditedEntity().setDocumentExtension(extension.toUpperCase(Locale.ROOT));
}

@Subscribe(“downloadButton”)
public void onDownloadButtonClick(Button.ClickEvent event) {
documentsTable.getSelected();
downloader.download(
documentsTable.getSingleSelected().getFile(),
documentsTable.getSingleSelected().getName(),
DownloadFormat.getByExtension(documentsTable.getSingleSelected().getDocumentExtension())
);
}

As DEFAULT_FORMATS is unmodifiableList I couldn’t add new DownloadFormat. I created a new method by using new List. It works same way but with option to add custom DownloadFormat’s.

public DownloadFormat getFormatByExtension(String extension) {

    DownloadFormat pptx = new DownloadFormat("pptx-mime-type", "pptx");

    if (StringUtils.isEmpty(extension)) {
        return OCTET_STREAM;
    }

    String extLowerCase = StringUtils.lowerCase(extension);

    List<DownloadFormat> formats = DEFAULT_FORMATS;
    List<DownloadFormat> formatList = new ArrayList<>(formats);
    formatList.add(pptx);
    
    for (DownloadFormat f : formatList) {
        if (f.getFileExt().equals(extLowerCase))
            return f;
    }
    return OCTET_STREAM;
}

Thanks for the idea, I’ve created an issue: https://github.com/Haulmont/jmix-core/issues/234

Also, please use triple backquotes (```) for formatting code here on the forum.

1 Like