0

So using https://developer.api.autodesk.com/oss/v2/buckets/:bucketKey/objects/:objectName and javascript - I have returned a binary image that will not save. I have used fs, btoa, base64 and a few others but all the saved files error with unrecognized format.

Image Error No matter what i try i get this error, except if i use postman and use saveas. I have been searching why this works in postman but i cannot seem to find any reliable answer. So

this is what i have

try {
        var data = null;
        var access_token = JSON.parse(Atoken);
        var settingspho = {
            "url": "https://developer.api.autodesk.com/oss/v2/buckets/wip.dm.prod/objects/" + urnid,
            "method": "GET",            
            "timeout": 0,
            "headers": {
                "Authorization": "Bearer " + access_token.access_token,
                "Content-Type": "application/json"                
            },
        };

        $.ajax(settingspho).done(function (response, body) {

           

           var data = 'data:image/jpeg;base64,' + btoa(response);
           \\ tried this too \\var buf = new Buffer(data, 'base64');
            

            const jsonfile = './photos/' + urnid;
            
          
            fs.writeFile(jsonfile,buf, function (err) {
                if (err) throw err;
            });
            
        });
        
    }    
    catch (err) {
        alert(err);
    }

Here is a snapshot of the responsePostman Body Response

2 Answers2

0

To download binary data from the Forge OSS service with jQuery, you need a custom Ajax transports of the jQuery, and here is a working example for you:

https://stackoverflow.com/a/50889959/7745569

BTW, the object name of the item stored on BIM360 is GUID base, e.g. eae32ae2-ed34-41c2-8cfd-7bb9919fddcb.jpg, 066edf4c-e546-4cf9-bb6a-4c93968dad00.dwg or 5df33424-1ee4-49fe-bf0f-8ca5b9642cd1.rvt. You cannot use the filename directly.

Eason Kang
  • 6,155
  • 1
  • 7
  • 24
  • Eason, Thank-you I have previously read your response and as i am already returning Binary data with the code above. However it may provide some insight to whats happening in the save process. I fail to see why Autodesk would want us to create a custom transport to solve this issue. The API documentation clearly states data is returned via binary so if its an issue with the oss bucket then Autodesk should stand up and fix it. – Littlerubarb Jan 13 '20 at 17:15
  • @Littlerubarb No, that is required by the jQuery, since jQuery cannot parse binary data to `blob` correctly with my investigation. – Eason Kang Jan 14 '20 at 00:54
  • Ok, so bear with me for a bit here, Im not one to accept a solution cause it works i need to understand the errors of my ways. So agreed ajax-JQuery cannot parse to a blob, did you try xhr.responseType = 'blob'; i was exploring this route yesterday, i know its pretty much the same thing but... – Littlerubarb Jan 14 '20 at 16:00
  • Could you share more details about the problem you're facing? I test it at my end and it works as expected. I also tried to make it returns `arraybuffer`, and it works fine. – Eason Kang Jan 15 '20 at 10:17
  • Code: https://gist.github.com/yiskang/528be6a929687dc1c2aaf1669e645ef1 – Eason Kang Jan 15 '20 at 10:17
  • Screencast: https://knowledge.autodesk.com/community/screencast/19c0e880-0902-4e8a-bcdc-a62dd12584fa – Eason Kang Jan 15 '20 at 10:17
0

So after some tinkering and the post by Eason here's is how to download the files from oss..

 try {

        var access_token = JSON.parse(Atoken);
        var xhr = new XMLHttpRequest();
        xhr.withCredentials = true;

        xhr.addEventListener("readystatechange", function () {
            if (this.readyState === 4 && this.status === 200) { 

                

                var res = this.response; 
                const jsonfile = './photos/'+ urnid; 
               
                try {
                                                         
                    fs.writeFile(jsonfile, toBuffer(res), function (err) {
                        if (err) throw err;
                    });


                }                               
                 catch (err) {
                     alert(err);
                 }
         
               


            }
        });

        xhr.open("GET", "https://developer.api.autodesk.com/oss/v2/buckets/wip.dm.prod/objects/" + urnid,);
        xhr.setRequestHeader("Authorization", "Bearer " + access_token.access_token);
        xhr.setRequestHeader("Content-Type", "application/octet-stream");
        xhr.responseType = 'arraybuffer';
        xhr.send();

        

       
        
    }    
    catch (err) {
        alert(err);
    }
};

function toBuffer(ab) {
    var buf = Buffer.alloc(ab.byteLength);
    var view = new Uint8Array(ab);
    for (var i = 0; i < buf.length; ++i) {
        buf[i] = view[i];
    }
    return buf;
}