openui5 FileUploader无法正常工作

时间:2017-09-06 17:36:31

标签: file-upload sapui5

我正在尝试使用简单的表单上传文件。我可以选择一个文件但是当我点击“上传”时没有任何反应。

我的FileUploader.view.xml就像:

<mvc:View
controllerName="sap.ui.unified.sample.FileUploaderBasic.Controller"
xmlns:l="sap.ui.layout"
xmlns:u="sap.ui.unified"
xmlns:mvc="sap.ui.core.mvc"
xmlns="sap.m"
class="viewPadding">
<l:VerticalLayout>
    <u:FileUploader
        id="fileUploader"
        name="myFileUpload"
        uploadUrl="upload/"
        width="400px"
        tooltip="Upload your file to the local server"
        uploadComplete="handleUploadComplete"/>
    <Button
        text="Upload File"
        press="handleUploadPress"/>
</l:VerticalLayout>

我的Contoller.controller.js

sap.ui.define(['sap/m/MessageToast','sap/ui/core/mvc/Controller'],
function(MessageToast, Controller) {
"use strict";

var ControllerController = Controller.extend("sap.ui.unified.sample.FileUploaderBasic.Controller", {
    handleUploadComplete: function(oEvent) {
        var sResponse = oEvent.getParameter("response");
        if (sResponse) {
            var sMsg = "";
            var m = /^\[(\d\d\d)\]:(.*)$/.exec(sResponse);
            if (m[1] == "200") {
                sMsg = "Return Code: " + m[1] + "\n" + m[2] + "(Upload Success)";
                oEvent.getSource().setValue("");
            } else {
                sMsg = "Return Code: " + m[1] + "\n" + m[2] + "(Upload Error)";
            }

            MessageToast.show(sMsg);
        }
    },

    handleUploadPress: function(oEvent) {
        var oFileUploader = this.getView().byId("fileUploader");
        oFileUploader.upload();
    }
});

return ControllerController;

});

当我在调试器中运行它时,我得到一个未捕获的TypeError:

Uncaught TypeError: Cannot read property '1' of null
at f.handleUploadComplete (FileUploader.controller.js?eval:11)
at f.a.fireEvent (EventProvider-dbg.js:229)
at f.a.fireEvent (Element-dbg.js:427)
at f.fireUploadComplete (ManagedObjectMetadata-dbg.js:426)
at HTMLIFrameElement.eval (FileUploader.js?eval:6)
at HTMLIFrameElement.dispatch (jquery-dbg.js:4737)
at HTMLIFrameElement.c3.handle (jquery-dbg.js:4549)

 if (m[1] == "200") {
                sMsg = "Return Code: " + m[1] + "\n" + m[2] + "(Upload Success)";
                oEvent.getSource().setValue("");

我搜索了示例代码,似乎我的代码没问题,但我不知道为什么我无法通过点击按钮上传文件。

1 个答案:

答案 0 :(得分:0)

我解决了这个问题! :) 因此,不要使用

var m = /^[(\d\d\d)]:(.)$/.exec(sResponse); 

在视图FileUploader中使用它:

sendXHR="true" 

之后,在控制器js上,您可以使用状态:

oEvent.getParameter("status")

并以json文件的形式获取答案:

 var jsonfile = JSON.parse(oEvent.getParameter("responseRaw"));

然后从服务器访问您发送的对象属性(仅当使用dataType:'json'时)

jsonfile.attribute

我希望这会有所帮助!