无法使用SAPUI5中的UploadCollection上传文件

时间:2016-08-01 14:28:07

标签: odata sap sapui5

当我尝试使用UploadCollection上传文件时,我收到403 Forbidden错误。 我的view.js中的代码是:

      var oOUpload = new sap.m.UploadCollection("oinspupload",{
        multiple : true,
        sameFilenameAllowed : true,
        instantUpload : false,
        uploadUrl :  "/sap/opu/odata/sap/ZACCBILL_SRV/FileSet",
/*      uploadComplete : function(oEvent){
            //alert ("File Uploaded successfully");
        //  oController.fileUpload(oEvent);
        }, */
        fileDeleted : function(oEvent){
            oController.fileDelete(oEvent);
        },
        fileRenamed : function(oEvent){
            alert ("File renamed successfully");
            //oController.fileRename(oEvent);
        }
    }); 

我的view.controller中的代码是:

         OData.request({
                requestUri : sServiceUrl,
                method : "GET",
                headers : 
                {
                    "X-Requested-With" : "XMLHttpRequest",
                    "Content-Type" : "application/atom+xml",
                    "DataServiceVersion" : "2.0",
                    "Authorization" : AuthToken,
                    "X-CSRF-Token" : "Fetch"
                }

            },
            function(data, response) {
                debugger;
                if(sap.ui.Device.browser.chrome || sap.ui.Device.browser.msie || sap.ui.Device.browser.safari){
                    header_xcsrf_token = response.headers['x-csrf-token'];
                }else if(sap.ui.Device.browser.firefox){
                    header_xcsrf_token = response.headers['X-CSRF-Token']; 
                }
                xcsrf_token_ref.header_xcsrf_token = header_xcsrf_token;
                csrftoken = xcsrf_token_ref.header_xcsrf_token; 
            debugger;
            uploadattachments(xcsrf_token_ref);
            },
            function(err) {
                debugger;
                var request = err.request; // the request that was sent. 
                var response = err.response; // the response that was received. 
                alert("Error in Get -- Request "
                        + request + " Response "
                        + response);
            });


  function uploadattachments(token){
debugger;
    var uploader;           
        uploader= sap.ui.getCore().byId("oinspupload"); 

         var aItems = uploader.getItems();
          var slug, sequence;
          for (i = 0; i < aItems.length; i++) { 
            sequence =  i + 1;
            slug = "CONTAINERID1000040100;STATUSIB;SEQUENCE" + sequence+          ";FILENAMECamera.png" ;
    uploader.addHeaderParameter(new sap.m.UploadCollectionParameter({name: "slug", value: slug }));  
    debugger;
    uploader.addHeaderParameter(new sap.m.UploadCollectionParameter({name: "X-Csrf-Token", value: token.header_xcsrf_token }));

    uploader.upload();
}
}

请注意缺少的括号,因为上面的代码不是完整的代码。

以上代码适用于fileuploader。我确定问题是uploadcollection没有正确传递获取的CSRF令牌,但我无法弄清楚出了什么问题。

2 个答案:

答案 0 :(得分:0)

终于在以下博客的帮助下找到了解决方案 http://scn.sap.com/community/developer-center/front-end/blog/2016/03/29/using-the-uploadcollection-to-uploaddownload-archivelink-files-via-gateway

上传集合仅适用于instantUpload为true,不适用于版本1.32.X的instantUpload为false。 UploadCollection是Buggy,未来的版本还有待修正。 CSRF令牌验证也需要在change事件中完成。以下是代码:

View.js

    var oOUpload = new sap.m.UploadCollection("oinspupload",{
        multiple : true,
        sameFilenameAllowed : false,
        instantUpload : true,
        uploadUrl :  "/sap/opu/odata/sap/ZACCBILL_SRV/FileSet",
        fileDeleted : function(oEvent){
            oController.fileDelete(oEvent);
        },
        fileRenamed : function(oEvent){
            alert ("File renamed successfully");
        },
        change: function(oEvent) {  
            debugger;
            csrftoken = xcsrf_token_ref.header_xcsrf_token; 
            var oUploadCollection = oEvent.getSource();  
            var oCustomerHeaderToken = new sap.m.UploadCollectionParameter({  
                name : "x-csrf-token",  
                value :  csrftoken  
            });  
            oUploadCollection.addHeaderParameter(oCustomerHeaderToken);
        }, 
    }); 

答案 1 :(得分:0)

所有标题参数必须添加到&#34;更改&#34;功能。如果你之后添加它们,它们将无法在Backend上收到。

此外,可以使用instantUpload = false上传文件。您只需要将uploadUrl参数与视图模型的参数绑定,动态地,当您更改网址时它会发生变化。

例如:

查看元素:

<UploadCollection instantUpload="false" uploadUrl="{ResourceModel>/sServiceUrl}"/>

Controller onInitFunction:

var resourcemodel = this.getOwnerComponent().getModel("ZGW_PURCHREQ_01_SRV");
var oDataResource = {
    sServiceUrl: resourcemodel.sServiceUrl + "/FileSet"
};
var jsonResource = new JSONModel(oDataResource);
this.getView().setModel(jsonResource, "ResourceModel");

当您点击上传时,它会向在&#34; sServiceUrl&#34;上定义的uploadUrl发送请愿书。 &#34; ResourceModel&#34;。

其他选项是在上传火灾上传功能之前设置上传网址和/或新标题参数:

    var oUploadCollection = this.getView().byId("UploadCollection");
    var sServiceUrl = resourcemodel.sServiceUrl + "/FileSet";
    var headerBanfn = null;
    for (var i = 0; i < oUploadCollection._aFileUploadersForPendingUpload.length; i++) {
        headerBanfn = new sap.ui.unified.FileUploaderParameter({
                            name: "banfn",
                            value: "123456"
                      });
        oUploadCollection._aFileUploadersForPendingUpload[i].setUploadUrl(sServiceUrl);
        oUploadCollection._aFileUploadersForPendingUpload[i].addHeaderParameter(headerBanfn);
    }
    oUploadCollection.upload();

我希望它很有用。