Quantcast
Channel: SCN: Message List - SAPUI5 Developer Center
Viewing all articles
Browse latest Browse all 8124

Re: FileUploader and X-CSRF-Token?

$
0
0

Hi Daniel

Further to the suggestion i made in  http://scn.sap.com/thread/3383994 that I didn't think this was possible with the FileUploader control, I found some time today to investigate further.

 

I tried to use a proxy to set the http headers like Jibin Joy suggested in http://scn.sap.com/thread/3262540 the issue like he found was that the data that is posted is encoded as multipart/form-data and the Gateway service is expecting a media resource with the correct content type eg image/gif.

 

I managed to get the FileLoader control posting successfully to Gateway services by extending the control, replacing the form.submit with an AJAX call.

sap.ui.commons.FileUploader.extend("ODataFileLoader", {
...     upload : function() {
...                    var file = jQuery.sap.domById("upload_1-fu").files[0];                    var oRequest = sap.ui.getCore().getModel(this.getModelName())._createRequest();                    var oHeaders = {                              "x-csrf-token": oRequest.headers['x-csrf-token'],                              "slug": this.getSlug(),                    };                     jQuery.ajax({                              type: 'POST',                              url: this.getUploadUrl(),                              headers: oHeaders,                              cache: false,                              contentType: false,                              processData: false,                              data: file,                              success: _handleSuccess,                              error: _handleError                    });

The code gets the CSRF token from a nominated Model and uses this with a Slug header that can be dynamically set.

 

You can find a working example at https://github.com/jasper07/sapui5bin/blob/master/ODataFileUpload.html

 

Cheers

John P


Viewing all articles
Browse latest Browse all 8124

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>