【问题标题】:SAPUI5 file upload download gets corruptedSAPUI5 文件上传下载损坏
【发布时间】:2017-09-22 09:37:58
【问题描述】:

谁能帮帮我。 我已经在 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
            }

        },

我在这里做错了什么?

【问题讨论】:

    标签: download upload sapui5 binaryfiles


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2020-09-27
      • 2015-07-14
      • 2015-07-16
      • 2017-12-04
      • 2019-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-13
      相关资源
      最近更新 更多