Custom Authorization Header for Upload?

17 posts, 0 answers
  1. Heather Kyle
    Heather Kyle avatar
    6 posts
    Member since:
    Oct 2009

    Posted 10 Dec 2012 Link to this post

    Is it possible to set a custom Authorization header for the Kendo Upload to send as part of the request when a file is uploaded? I have seen several forum posts and the getting started article about adding custom information to the content of the POST itself, but this is not what I need. The controller to which I am uploading files is designed to process specific authorization information when it is packed in the header, so I am looking for something similar to what ajax provides for in its beforeSend event handler:
    $.ajax({
      type: "PUT",
      url: URL_FOR_UPLOAD,
      beforeSend: function (xhr) {
       xhr.withCredentials = true;
       xhr.setRequestHeader('Authorization', CUSTOM_AUTHORIZATION_HERE);
    }
    Can this be done?

    Thank you.
    Heather
  2. T. Tsonev
    Admin
    T. Tsonev avatar
    2772 posts

    Posted 12 Dec 2012 Link to this post

    Hi,

    Setting a header is technically possible when the file is uploaded using the File API. The upload however does not expose the raw XMLHttpRequest at an appropriate moment.

    We can fix this, but you still won't be able to set headers in older browsers and IE prior to version 10. These browsers rely on the legacy IFRAME method that does not provide control over the headers.

    Please, let us know if you want to proceed despite this limitation. We will then proceed to expose the XHR object in the upload event arguments.

    Note that the upload is already setting the withCredentials option to true.

    Regards,
    Tsvetomir Tsonev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. Kendo UI is VS 2017 Ready
  4. T. Tsonev
    Admin
    T. Tsonev avatar
    2772 posts

    Posted 29 Mar 2013 Link to this post

    Hello,

    After some deliberation we decided to add the XHR to the upload event arguments. It can be quite useful, when available.

    The update will be included in the internal build later today. Here's a sample how to set a custom header:

    function onUpload(e) {
        var xhr = e.XMLHttpRequest; 
        xhr.addEventListener("readystatechange", function(e) {
            if (xhr.readyState == 1 /* OPENED */) {
                xhr.setRequestHeader('Authorization', CUSTOM_AUTHORIZATION_HERE);
            }
        });
        kendoConsole.log("Upload :: " + getFileInfo(e));
    }

    I hope this helps.

    All the best,
    Tsvetomir Tsonev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  5. Heather Kyle
    Heather Kyle avatar
    6 posts
    Member since:
    Oct 2009

    Posted 29 Mar 2013 Link to this post

    That does help tremendously. Thank you for making this change.
  6. T. Tsonev
    Admin
    T. Tsonev avatar
    2772 posts

    Posted 01 Apr 2013 Link to this post

    Hello,

    I'm glad this helps.

    Please note that we should check "xhr" for null before use. I've added an updated code sample to the docs:

    function onUpload(e) {
        var xhr = e.XMLHttpRequest;
        if (xhr) {
            xhr.addEventListener("readystatechange", function(e) {
                if (xhr.readyState == 1 /* OPENED */) {
                    xhr.setRequestHeader("X-Foo", "Bar");
                }
            });
        }
    }


    Greetings,
    Tsvetomir Tsonev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  7. Brett
    Brett avatar
    1 posts
    Member since:
    Aug 2014

    Posted 19 Aug 2014 in reply to T. Tsonev Link to this post

    Hi Telerik-  Is there any way you could add the XHR to the "remove" events argument as well?  I need to add a CSRF token to the header of the request just like you set on the upload events.  Its coming back as undefined, so I'm guessing you didn't expose it in all methods.  
  8. T. Tsonev
    Admin
    T. Tsonev avatar
    2772 posts

    Posted 21 Aug 2014 Link to this post

    Hello,

    This definitely makes sense. We'll investigate, but for the moment you can use the global jQuery Ajax handlers.

    Apologies for the caused inconvenience.

    Regards,
    T. Tsonev
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  9. RES
    RES avatar
    77 posts
    Member since:
    Sep 2010

    Posted 22 Jan 2015 in reply to T. Tsonev Link to this post

    I'm using the example provided in the docs below to add an authorization header but I am noticing the event listener is getting called multiple times with a readyState of 1.
    http://docs.telerik.com/kendo-ui/api/javascript/ui/upload#events-upload

    The first time it gets called you can of course successfully add the header but the next time it gets called it throws back  JavaScript runtime error: InvalidStateError.

    I can overcome this by storing a flag that indicates the headers have already been added but why not just add some sugar to the upload widget for adding headers so we don't have to keep track of whether headers have already been added to the request?
  10. Dimiter Madjarov
    Admin
    Dimiter Madjarov avatar
    2159 posts

    Posted 23 Jan 2015 Link to this post

    Hello,


    We have met this strange issue before. It turned out that Internet Explorer fires readystatechange twice with readyState 1 and the second execution of the handler causes the error. I assume that this could also be reproduced outside of the Kendo UI Upload widget environment with a standard XMLHttpRequest. Nevertheless as a workaround for the the current case you could name the executed handler and unbind it after the first execution.
    E.g.
    function onUpload(e) {
        var xhr = e.XMLHttpRequest;
     
        if (xhr) {
            xhr.addEventListener("readystatechange", function onReady(e) {
                if (xhr.readyState == 1 /* OPENED */) {
                    xhr.setRequestHeader("foo", "bar");
     
                    xhr.removeEventListener("readystatechange", onReady);
                }
            });
        }
    }

    Have a great day!

    Regards,
    Dimiter Madjarov
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  11. Thomas
    Thomas avatar
    12 posts
    Member since:
    Jul 2013

    Posted 31 Aug 2015 in reply to T. Tsonev Link to this post

    Hello there, a year has passed since your last response. Did you have any news for that?

    Using global ajax handlers fixed my problem but it makes my code a bit messy and I was wondering if anything changed since last year.

     

    Thank you.

  12. Dimiter Madjarov
    Admin
    Dimiter Madjarov avatar
    2159 posts

    Posted 31 Aug 2015 Link to this post

    Hello Thomas,

    If you are referring to the last mentioned issue is not Kendo UI Upload related, but Internet Explorer related, as it fires the  readystatechange twice with readyState 1. As for the previous one, at the moment we do not provide access to the XHR in the remove event handler.

    Regards,
    Dimiter Madjarov
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  13. Thomas
    Thomas avatar
    12 posts
    Member since:
    Jul 2013

    Posted 01 Sep 2015 in reply to Dimiter Madjarov Link to this post

    I was referring at the XHR in the remove event handler.
  14. Dimiter Madjarov
    Admin
    Dimiter Madjarov avatar
    2159 posts

    Posted 02 Sep 2015 Link to this post

    Hello Thomas,

    At the moment the XHR is not available in the remove event handler. I will forward the request for review to the developers team.

    Regards,
    Dimiter Madjarov
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  15. Thomas
    Thomas avatar
    12 posts
    Member since:
    Jul 2013

    Posted 18 Jul Link to this post

    Any news on the remove event handler?
  16. Dimiter Madjarov
    Admin
    Dimiter Madjarov avatar
    2159 posts

    Posted 18 Jul Link to this post

    Hello Thomas,

    We have this logged for future implementation, but don't have an exact time span to state.

    Regards,
    Dimiter Madjarov
    Telerik by Progress
     
    Get started with Kendo UI in days. Online training courses help you quickly implement components into your apps.
     
  17. Thomas
    Thomas avatar
    12 posts
    Member since:
    Jul 2013

    Posted 03 Nov Link to this post

    Is there a github link for that issue so that I can be informed about it's progress? Thank you.
  18. Dimiter Madjarov
    Admin
    Dimiter Madjarov avatar
    2159 posts

    Posted 03 Nov Link to this post

    Hello Thomas,

    Here is the GitHub issue for the mentioned case. We have this in our short term development plans for the next Q.

    Regards,
    Dimiter Madjarov
    Telerik by Progress
     
    Build rich, delightful, *native* Angular 2 apps with Kendo UI for Angular 2. Try it out today! Kendo UI for Angular 2 (currently in beta) is a jQuery-free toolset, written in TypeScript, designed from the ground up to offer true, native Angular 2 components.
     
Back to Top
Kendo UI is VS 2017 Ready