sapui5如何在控制器中读取PDF文件内容

问题描述 投票:1回答:1

我正面临PDF文件上传的问题..

enter image description here

在上面的屏幕截图中,如果你看到,当我试图上传PDF文件时,我无法读取该pdf文件中的内容。我的要求是,我需要从该文件中获取内容作为String以及我需要发送到后端服务器的内容。

如果我试图阅读内容,我得到以下错误

HTTP状态405 - 错误的Methodenter image description here

以下是我的代码..

我使用xmlns:u =“sap.ui.unified”库

<u:FileUploader id="fileUploader" name="myFileUpload" tooltip="Upload Service Sheet"
                                                uploadComplete="handleUploadComplete" change="handleValueChange" typeMissmatch="handleTypeMissmatch" style="Emphasized" fileType="pdf"
                                                placeholder="Choose a file for Upload..." maximumFileSize="2000" mimeType="pdf" buttonText="Upload">
                                            </u:FileUploader>

handleUploadComplete: function(oEvent) {
            var fileName = oEvent.getSource().getProperty("value");
            var sResponse = oEvent.getParameter("response");
            if (sResponse) {
                var sMsg = "";
                var m = /^\[(\d\d\d)\]:(.*)$/.exec(sResponse);
                if (m[0] == "200") {
                    sMsg = "Return Code: " + m[0] + "(Upload Success)";
                    oEvent.getSource().setValue("");
                } else {
                    sMsg = "Return Code: " + m[0] + "(Upload Error)";
                }

                MessageToast.show(sMsg);
            }

        },

有人可以帮助我如何阅读PDF中的数据?

先感谢您

sapui5
1个回答
6
投票

看看这个例子。希望这可以帮助。

视图

    <u:FileUploader change="onChange" fileType="pdf" mimeType="pdf" buttonText="Upload" />

调节器

    convertBinaryToHex: function(buffer) {
        return Array.prototype.map.call(new Uint8Array(buffer), function(x) {
            return ("00" + x.toString(16)).slice(-2);
        }).join("");
    },


    onChange: function(oEvent){
        var that = this;
        var reader = new FileReader();
        var file = oEvent.getParameter("files")[0];

        reader.onload = function(e) {
            var raw = e.target.result;
            var hexString = that.convertBinaryToHex(raw).toUpperCase(); 
            // DO YOUR THING HERE            
        };

        reader.onerror = function() {
            sap.m.MessageToast.show("Error occured when uploading file");
        };

        reader.readAsArrayBuffer(file);
    },
© www.soinside.com 2019 - 2024. All rights reserved.