0

i'm trying to upload my files as form-data, after i've created a scene. But I receive always the error "Specified Photoscene ID doesn't exist in the database" (which were created directly before).

My upload function:

// Upload Files
async function uploadFiles(access_Token, photoSceneId, files) {
    try {

        const params = new URLSearchParams({
            'photosceneid': photoSceneId,
            'type': 'image',
            'file': files
        })

        const headers = Object.assign({
            Authorization: 'Bearer ' + access_Token,
            'Content-Type': 'multipart/form-data' }, 
            files.getHeaders()
        )
        
        let resp = await axios({
            method: 'POST',
            url: 'https://developer.api.autodesk.com/photo-to-3d/v1/file',
            headers: headers,
            data: params

        })
        let data = resp.data;
        return data;
    } catch (e) {
        console.log(e);
    }
};

I've also tried a few varaints, e.g. adding the photosceneId to the form data (form.append(..), but doesn't works either.

Any helpful suggestion are appreciated. Thx in advance.

zieho
  • 1
  • 1

2 Answers2

0

There might be two problems here. First, I am not sure of it, as I don't have experience of URLSearchParams as a "packer" for POST requests. This might be the reason why you get "Specified Photoscene ID doesn't exist in the database" error - perhaps the way the data are serialized using URLSearchParams is not compatible.

The second problem, I am sure of it, is regarding the way you submit the files. According to documentation, you have to pass the files one by one, like

  "file[0]=http://www.autodesk.com/_MG_9026.jpg" \
  "file[1]=http://www.autodesk.com/_MG_9027.jpg"

and not just passing an array to the "file" field.

Having this said, try this approach:

var axios = require('axios');
var FormData = require('form-data');
var fs = require('fs');
var data = new FormData();
var TOKEN = 'some TOKEN';
const photoSceneID = 'some_photoscene_id';

data.append('photosceneid', photoSceneID);
data.append('type', 'image');
data.append('file[0]', fs.createReadStream('/C:/TEMP/Example/DSC_5427.JPG'));
data.append('file[1]', fs.createReadStream('/C:/TEMP/Example/DSC_5428.JPG'));
data.append('file[2]', fs.createReadStream('... and so on ...'));

var config = {
  method: 'post',
  url: 'https://developer.api.autodesk.com/photo-to-3d/v1/file',
  headers: { 
    'Content-Type': 'application/x-www-form-urlencoded', 
    'Authorization': 'Bearer ' + TOKEN, 
  },
  data : data
};

axios(config)
.then(function (response) {
  console.log(JSON.stringify(response.data));
})
.catch(function (error) {
  console.log(error);
});

Also, I always recommend instead of jumping right into the code, to check first the workflow using apps like Postman or Insomnia and then, after you validated the workflow (created the photoscene, all images were properly uploaded and so on), you can translate this into the code.

At the end of this blogpost you will find the link to alrady created Postman collection, but I highly recommend building your own collection, as part of the learning step.

denis-grigor
  • 505
  • 5
  • 9
  • First of all, thank you very much for your help. It was actually the problem that I used the URLParams as packer. On the other hand I had not considered the "photosceneid" and the "type" in the FormData. Thanks again for the help & happy new year! – zieho Jan 04 '22 at 15:57
0

This is the solution that worked for me. Please note that the upload should be limited by a maximum of 20 files per call.

// Upload Files
async function uploadFiles(access_Token, photoSceneId) {
    try {

        let dataPath = path.join(__dirname, '../../data')
        let files = fs.readdirSync(dataPath)
    
        var data = new FormData();

        data.append('photosceneid', photoSceneId)
        data.append('type', 'image')

        for(let i=0; i < files.length; i++) {

            let filePath = path.join(dataPath, files[i])
            let fileName = 'file[' + i + ']'

            data.append(fileName, fs.createReadStream(filePath))
            
        }

        const headers = Object.assign({
            Authorization: 'Bearer ' + access_Token,
            'Content-Type': 'multipart/form-data;boundary=' + data.getBoundary()}, 
            data.getHeaders()
        )
        
        let resp = await axios({
            method: 'POST',
            url: 'https://developer.api.autodesk.com/photo-to-3d/v1/file',
            headers: headers,
            maxContentLength: Infinity,
            maxBodyLength: Infinity,
            data: data

        })
        let dataResp = resp.data;
        return dataResp;
    } catch (e) {
        console.log(e);
    }
};
zieho
  • 1
  • 1