www.webdeveloper.com
Results 1 to 4 of 4

Thread: downloading file with XMLHTTPRequest and Mozilla

Hybrid View

  1. #1
    Join Date
    Nov 2013
    Posts
    2

    downloading file with XMLHTTPRequest and Mozilla

    Greetings ,

    I am trying to download a file with cross domain ajax here is the script :

    Code:
      function getXDomainRequest() {
           var xdr = null;
         
           if (window.XDomainRequest) {     
            xdr = new XDomainRequest();
           } else if (window.XMLHttpRequest) {  
            xdr = new XMLHttpRequest();
           } else {
            alert("Votre navigateur ne gère pas l'AJAX cross-domain !");
          }
         
          return xdr;
         } 
    
    
    
       var xhr = new getXDomainRequest();
        xhr.responseType = 'blob';
        xhr.onload = function() {
        var a = document.createElement('a');
        a.href = window.URL.createObjectURL(xhr.response); // xhr.response is a blob
        a.download = filename; // Set the file name.
        a.style.display = 'none';
        document.body.appendChild(a);
        a.click();
        delete a;
        xhr.open('GET', url,false);
        xhr.send();
    the getXDomainRequest() returns an XMLHTTPrequest ,and yet the script ends at the line
    Code:
    xhr.responseType = 'blob';
    ,wich is only supported by version 2 of xhr,does that mean my browser does not support it xhr 2,i'm using Mozilla firefox 25 how come?

    If anyone catches the gap in this please let me know.

  2. #2
    Join Date
    Nov 2013
    Location
    London, United Kingdom
    Posts
    15
    This example shows that you first need to open the request and then specify the responseType: https://developer.mozilla.org/en-US/...ng_Binary_Data

  3. #3
    Join Date
    Nov 2013
    Posts
    2
    Hi ,

    Not only you have to open the request first but also to specify true for the last argument emphasizing that it is an asynchronous request,otherwise setting responseType ends the script,wich is a reason why they needed to open request first,but why are those two factors related?

    However,i found out i have one more issue ,i'm actually sending cross domain requests and i checked the response headers they don't include :Access-Control-Allow-Origin header,so i'll never be authorized to download these files with ajax,i'll just have to open
    the urls in popups and that dosen't give me more control over downloads,can you suggest to me a proper way to scan through a group of file urls to download them with good way to manage the downloads.

  4. #4
    Join Date
    Nov 2013
    Location
    London, United Kingdom
    Posts
    15
    Not only you have to open the request first but also to specify true for the last argument emphasizing that it is an asynchronous request,otherwise setting responseType ends the script,wich is a reason why they needed to open request first,but why are those two factors related?
    There are actually two issues. First is that FF requires responseType being specified after open(). Probably it is because of the bug in FF. Second is that the responseType is allowed only for asynchronous requests: http://www.w3.org/TR/XMLHttpRequest/...type-attribute

    As far as I know there is no way to perform cross-domain ajax request with the XMLHttpRequest object. The only workaround is jsonp but is probably not what you need.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles