CMS Development

ymleyva
Member

Uploading pdf file using CMS Files API

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
9 Replies 9
ymleyva
Member

Uploading pdf file using CMS Files API

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
tjoyce
Recognized Expert | Elite Partner
Recognized Expert | Elite Partner

Uploading pdf file using CMS Files API

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

H_Al-Azzawi
Member

Uploading pdf file using CMS Files API

I tried it , and it worked for me, thanks

0 Upvotes
ymleyva
Member

Uploading pdf file using CMS Files API

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 | Elite Partner
Recognized Expert | Elite Partner

Uploading pdf file using CMS Files API

@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

Uploading pdf file using CMS Files API

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

0 Upvotes
dennisedson
HubSpot Product Team
HubSpot Product Team

Uploading pdf file using CMS Files API

@tjoyce  anything standing out to you?

0 Upvotes
dennisedson
HubSpot Product Team
HubSpot Product Team

Uploading pdf file using CMS Files API

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?

0 Upvotes
ymleyva
Member

Uploading pdf file using CMS Files API

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