majki
majki

Reputation: 33

How to upload file in hybris backoffice without uploading it to Media?

I want to create a custom widget in hybris backoffice, which would allow importing CSV files from disk, and the data from the CSV file will be used to insert data into the database. However, from what I've found, hybris allows to upload files only from its Media container.

https://help.hybris.com/6.0.0/hcd/8babc4778669101493e3e9234f73106a.html

"The Default File Upload Editor enables you to upload and download binary content from the existing Media."

It makes it pretty cumbersome - to firstly upload the file to the Media, and only then choosing the uploaded file from Media to use it in my custom widget.

Is it possible to overcome this and upload file directly from disk?

Upvotes: 0

Views: 3569

Answers (2)

Prakash Nataraju
Prakash Nataraju

Reputation: 1

Typical CSV file configuration in *backoffice-config.xml

            <wz:property qualifier="csvFile" type="com.hybris.cockpitng.editor.defaultfileupload.FileUploadResult" 
                             editor="com.hybris.cockpitng.editor.dndfileupload" validate="false">
                    <wz:editor-parameter>
                        <wz:name>accept</wz:name>
                        <wz:value>text/csv</wz:value>
                    </wz:editor-parameter>
            </wz:property>

Typical way to access them in your action (assuming, you're using the template bean in Backoffice wizard)

final DefaultWidgetModel productUploadModel = (DefaultWidgetModel) adapter.getWidgetInstanceManager().getModel();

        final ProductUploadWizardForm productUpload = productUploadModel.getValue("PRODUCT_UPLOAD",
                ProductUploadWizardForm.class);

            final FileUploadResult fileUploadResult = productUploadModel
                    .getValue("PRODUCT_UPLOAD" + ".csvFile",
                    FileUploadResult.class);
LOG.info("Value of fileUploadResult.name:" + fileUploadResult.getName());

LOG.info("Print the contents->"+new String(fileUploadResult.getData(), StandardCharsets.UTF_8));

Upvotes: 0

majki
majki

Reputation: 33

I'm answering my own question, someone might have a similar issue - it seems that above quote from Hybris documentation is misleading - if you use Default File Upload Editor it will allow you to choose a file from your disk.

e.g. in your widget's zul file:

<editor id="yourCustomEditorId" type="java.io.File" defaultEditor="com.hybris.cockpitng.editor.defaultfileupload" />

will create two default buttons - upload and reset

in your controller you will add

private Editor yourCustomEditorId;

from which you can get FileUploadResult:

FileUploadResult fileUploadResult = (FileUploadResult) yourCustomEditorId.getValue();

Upvotes: 1

Related Questions