Uploading pdf file using CMS Files API

ymleyva
Member

Hello everyone, I am using the CMS Files api to upload a file and I have a problem.
when uploading a PDF file the file manager does not recognize the content type
End point: /filemanager/api/v3/files/upload

0 Upvotes
8 Replies 8
dennisedson
Community Manager

Hi @ymleyva 

Could you add your code that you are using for this?  Just so we can get a peek (no hapikey, please)

Also, curious if this is consistently happening with all pdf files or just this one?

Thanks,

Dennis




Check out our Community Developer Blog
where we feature our Community driven developer podcast and how to content
0 Upvotes
ymleyva
Member

Hello,
The problem happens to me with all the PDFs that I have tried.
Here my server code:
async sendCV(file) {
let url = `https://api.hubapi.com/filemanager/api/v3/files/upload?hapikey=${hubspotConfig.APIKEY}`;
let fileOptions = {
access: 'PUBLIC_INDEXABLE',
ttl: 'P3M',
overwrite: false,
duplicateValidationStrategy: 'NONE',
duplicateValidationScope: 'EXACT_FOLDER'
};

let formData = {
file: fs.createReadStream(file.path),
options: JSON.stringify(fileOptions),
folderPath: "files",
};
let urlFile = await new Promise((resolve, reject) => {
request.post({
url: url,
formData: formData
}, function optionalCallback(err, httpResponse, body) {
if (httpResponse) {
resolve({'url': JSON.parse(body).objects[0].url});
} else
reject({'status': 0, 'message': err});
});
});
}

Thanks for answering

0 Upvotes
dennisedson
Community Manager

@tjoyce  anything standing out to you?

Thanks,

Dennis




Check out our Community Developer Blog
where we feature our Community driven developer podcast and how to content
0 Upvotes
tjoyce
Recognized Expert | Platinum Partner

@ymleyva - how do you know it's not recognizing the file type? are you getting some sort of error? Try accessing the file preview link from a non-hubspot default domain.... The default domain does some clever things like strip mime types for javascript files and HTML files... if you access the preview link of a file from your own domain, it doesn't do this

ymleyva
Member

Sorry, about previewing the file ... when I try, the file downloads directly

0 Upvotes
ymleyva
Member

I show you how it looks in the file manager. I'm not getting the error, if I download the file and manually change the extension to .PDF, the file opens correctly.

Screenshot_2.png

Thank you for answer

0 Upvotes
tjoyce
Recognized Expert | Platinum Partner

@ymleyva - would it be as simple as making sure the file extension is present on the file name before you upload it?

ymleyva
Member

Hi,

The file reaches the server through a form-data, I don't get the .PDF extension in the file.path but I get the content-type as pdf. Here show you the file.path and file.type in the server:

console.png

Thank you for your answer

0 Upvotes