cancel
Showing results for 
Search instead for 
Did you mean: 

SAPUI5 file download upload corrupted

eachtime
Explorer
0 Kudos

Can someone help me. I've implemented a file upload / download in UI5 that seems to work but when I download the file it gets corrupted and I can't open it. For now I'm only testing with image files:

                new sap.ui.unified.FileUploader({
                    buttonOnly: true,
                    buttonText: "Upload files",
                    icon: "sap-icon://upload",
                    change: function(oEvent) {
                        var oFileUploader = oEvent.getSource();
                        oItem = oFileUploader.getParent().getParent().getParent();
                        var sPath = oItem.getBindingContext().getPath();
                        var files = oEvent.getParameter("files");
                        var file = files[0];
                        if (file) {
                            var oNewFile = {
                                ID: that.count++,
                                SurveyAnswerID: oSA.ID,
                                FileName: oEvent.getParameter("newValue"),
                                FileBinary: null,
                                MimeType: "image/jpeg",
                                Mode: "POST"
                            };
                            var reader = new FileReader();
                            reader.onload = function(evt) {
                                var binaryString = evt.target.result;
                                oNewFile.FileBinary = binaryString;
                            };
                            reader.readAsBinaryString(file);
                        } else {
                            oNewFile.FileBinary = "";
                            oNewFile.FileName = "";
                            MessageToast.show("Something went wrong with the file upload.\n Please try again");
                        }
                        that._pushItemToFileUploadModel(oNewFile.ID, oNewFile);
                        that._getFileUploadModel().refresh();
                    }
                })

Download code:

        selectionChange: function(oEvent) {
            var item = oEvent.getSource().getSelectedItem();
            var model = that._getFileUploadModel();
            if (item) {
                var a = window.document.createElement('a');
                a.href = window.URL.createObjectURL(new Blob([item.getDocumentId()], {
                    type: item.getMimeType()
                }));
                a.download = item.getFileName();
                // Append anchor to body.
                document.body.appendChild(a);
                a.click();
                // Remove anchor from body
                document.body.removeChild(a);
            }

            try {
                oEvent.getSource()._oList.removeSelections();
            } catch (e) {
                //DO nothing
            }

        },

Accepted Solutions (0)

Answers (2)

Answers (2)

jaganan
Explorer
0 Kudos

i had the same issue but only with excel files.. other files work (pdf, doc). somehow the problem was Fileloader does not support xls and xlsx when you call upload method. By now there should be a new control out there. My workaround was encoding to base64 when I create the content and passed it in the payload.

Make sure to disable create stream (disable media property) on the entity and let it just call create_entity method and update your backend table on oModel.create.

Downloading is fine with media property on calling CREATE_STREAM.

My download functionality was on another app and calling a different oData service. I just used the URLHelper. But if you use the same oData service, you can always create a different entity in the same oData for download with Media property on.

irfan_gokak
Contributor
0 Kudos

Hi Tiago,

You are not calling getStream to download a file???