简体   繁体   English

文件上传到 BIM 360 在 Postman 中工作,但不适用于 React JS

[英]File Upload to BIM 360 works in Postman but not with React JS

Hi I'm new to Autodesk Forge.嗨,我是 Autodesk Forge 的新手。 I was following the tutorial to upload a file to BIM 360. The upload works when using Postman but I'm unable to view the file when I upload using React JS.我按照教程将文件上传到 BIM 360。使用 Postman 时上传有效,但使用 React JS 上传时无法查看文件。

I'm using FileReader and reading file as BinaryString.我正在使用 FileReader 并将文件作为 BinaryString 读取。 Where token is the access token.其中 token 是访问令牌。

onInputChange(e){
    let files = e.target.files;
    let reader = new FileReader(); 
    reader.readAsBinaryString(files[0]);

    reader.onload = (e) => {
        const buffer = Buffer.from(e.target.result, 'base64');

        var myHeaders = new Headers();
        myHeaders.append("Authorization", `Bearer ${token}`);
        myHeaders.append("Content-Type", "application/octet-stream");

        var requestOpts = {
            method: 'PUT',
            headers: myHeaders,
            body: buffer,
        };

        
        fetch('https://developer.api.autodesk.com/oss/
            v2/buckets/:bucketKey/objects/:objectName'
            , requestOpts)
        .then(response => response.json())



    }
}

The file manages to upload but im unable to view it in bim 360. Image below.该文件设法上传,但我无法在 bim 360 中查看它。下图。

bim360 file image bim360 文件图像

Found the solution thanks to the suggestion in the comments.由于评论中的建议找到了解决方案。 Had to change the FileReader method to必须将 FileReader 方法更改为

reader.readAsArrayBuffer(files[0]);

The documentation found in https://github.com/Autodesk-Forge/forge-api-nodejs-client/blob/master/src/api/ObjectsApi.js shows that the body has to be an array buffer. https://github.com/Autodesk-Forge/forge-api-nodejs-client/blob/master/src/api/ObjectsApi.js中的文档显示主体必须是数组缓冲区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM