SAPUI5文件上载下载已损坏

时间:2017-09-22 09:37:58

标签: download upload sapui5 binaryfiles

有人能帮助我吗? 我已经在UI5中实现了文件上传/下载似乎可以正常工作,但是当我下载文件时它被破坏了,我无法打开它。 目前我只测试图像文件:

                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();
                    }
                })

下载代码:

        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
            }

        },

我在这做错了什么?

1 个答案:

答案 0 :(得分:0)

我解决了以这种方式转换文件的问题:

                    var u8_2 = new Uint8Array(atob(data).split("").map(function(c) {
                        return c.charCodeAt(0);
                    }));

                    var a = window.document.createElement('a');
                    a.href = window.URL.createObjectURL(new Blob([u8_2], {
                        type: item.getMimeType()
                    }));