添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Hey all!

I've been trying to uplaod a file to file manager using this endpoint

POST /filemanager/api/v3/files/upload

But idk what's wrong with my script, as I'm not reading from any file as given in example. I'm just getting the file in binary form from an Url and after buffer making it a readable stream object and passing it to the above mentioned end point

Here's my script:

async function upload () {
const file_path = url
const response = await axios . get ( file_path ,{ responseType : 'arraybuffer' });

if ( response . status == 200 ){
const buf = new Buffer . from ( response . data , "base64" );
const bs = new stream . PassThrough ();
console . log ( bs )

var fileOptions = {
access : 'PUBLIC_INDEXABLE' ,
ttl : 'P3M' ,
overwrite : false ,
duplicateValidationStrategy : 'NONE' ,
duplicateValidationScope : 'ENTIRE_PORTAL'
};

var formData = {
file : bs ,
options : JSON . stringify ( fileOptions ),
folderPath : 'docs'
};
axios . post ( postUrl , formData ). then ( function ( response ){
console . log ( "Test" )
console . log ( response )
})
};
return false
}

I was trying to download file from the url and then upload the binary data of that file to the Hubspot Api endpoint. But for the mentioned endpoint you can't directly upload that binary data, there must be a file in your system in which your data exists, then you directly upload that file from your system. For uploading file from the url, you have different endpoint which is " /files/v3/files/import-from-url/async"

I was trying to download file from the url and then upload the binary data of that file to the Hubspot Api endpoint. But for the mentioned endpoint you can't directly upload that binary data, there must be a file in your system in which your data exists, then you directly upload that file from your system. For uploading file from the url, you have different endpoint which is " /files/v3/files/import-from-url/async"

Hi @UmairKhan ,

Hi , although your code looks fine to me , but could you please share the response what you are getting after hitting the code. also can you please try this code for uploading the file var request = require('request');
var fs = require('fs');

var postUrl = ' https://api.hubapi.com/filemanager/api/v3/files/upload?hapikey=demo ';

var filename = 'example_file.txt';

var fileOptions = {
access: 'PUBLIC_INDEXABLE',
ttl: 'P3M',
overwrite: false,
duplicateValidationStrategy: 'NONE',
duplicateValidationScope: 'ENTIRE_PORTAL'
};

var formData = {
file: fs.createReadStream(filename),
options: JSON.stringify(fileOptions),
folderPath: 'docs'
};

request.post({
url: postUrl,
formData: formData
}, function optionalCallback(err, httpResponse, body){
return console.log(err, httpResponse.statusCode, body);
}); Refrence doc link : https://legacydocs.hubspot.com/docs/methods/files/v3/upload_new_file the output should look like this https://legacydocs.hubspot.com/docs/methods/files/get_files_file_id


Hope this helps!


If we were able to answer your query, kindly help the community by marking it as a solution.

Thanks and Regards.

Thanks @webdew for responding

My code is working fine if I use file in filename and use readStream to read the file. But whenever I get the  attachment from the external Url and using buffer or PassThrough stream, I send it to hubspot server I get this error

UnhandledPromiseRejectionWarning: Error: Request failed with status code 415

The HTTP 415 Unsupported Media Type client error response code indicates that the server refuses to accept the request because the payload format is in an unsupported format. The format problem might be due to the request's indicated Content-Type or Content-Encoding, or as a result of inspecting the data directly.
//START END-USER CONFIGURATION //------------------------------ //selectors for hover card triggers var allHoverCardTriggers = '.author-name-link,.friend-list .friend a,.username a,.avatar,.user-avatar,.author-img, .authors a, .messageauthorusername a, a.lia-user-name-link, .js-latest-post-by-from a, .user-online-list li a, a.UserAvatar, .customUsersOnline a, #authors a,.dashboard-followers a.user-name, .dashboard-following a.user-name,.author-login-wrapper a, .hb-leaderboard a, .author-img-floated'; // Forward calling page's URL params to endpoint URL as well, helps with testing! var params = (new URL(location.href)).searchParams; var userApiUrl = '/plugins/custom/hubspot/hubspot/hovercardendpoint?' + ((params.set('user_id', '') == []._) && params.toString()); if($('.hover-card-container').length<1){ $('body').append('
'); var cardWrapper = $('.hover-card-container'); var error = false; var thisUserID = ''; var thisUserLogin = ''; var userLink =''; var cardTimer; var leaveTimer; function mouseenter(Elem) { var thisEl = Elem; cardTimer = setTimeout(function(){ var docWidth = $(document).width(); var rightSide = false; var userLink = thisEl.attr('href'); if($('.ViewProfilePage').length && $('img.lia-user-avatar-profile',thisEl).length){thisUserID = '';} else if(thisEl.attr('href')=='#' || thisEl.attr('href')=='' || !userLink.match('viewprofilepage')){ return false;} else{ var thisLen = (userLink).split('/'); thisUserID = (thisLen)[thisLen.length-1]; var thisCard = $('.profileCard[data-user='+thisUserID+']',cardWrapper); var cardId = 'userProfileCard-'+ thisUserID; var addAttr = thisEl.attr('aria-describedby',cardId); var thisElTopOffset = Math.round(thisEl.offset().top+(thisEl.height()/2)+30); var thisElbottomoffset = "auto"; var className = ""; var winHeight = $(window).height(); var elOffset = thisEl.offset(); var scrollTop = $(window).scrollTop(); var elementOffset = thisEl.offset().top; var distanceTop = (elementOffset - scrollTop); var distanceBottom = (winHeight + scrollTop) - (elOffset.top + thisEl.outerHeight(true)); var distanceLeft = Math.round(thisEl.offset().left); var bodyHight = $('body').height(); var topParam = ''; var bottomparam = ''; var position = ''; var className = 'topArrow'; cardId if(distanceBottom < 300 ){ if(distanceLeft < 59){ thisCard.removeClass('bottomArrow'); var className = 'leftArrow'; var distanceLeft = (distanceLeft)+(39); var thisElTopOffset = (thisElTopOffset)-(150); }else{ var thisElTopOffset = (thisElTopOffset)-(301); var className = 'bottomArrow'; thisCard.removeClass('topArrow'); thisCard.removeClass('leftArrow'); var distanceLeft = (distanceLeft)-(45); else{ if(distanceLeft < 59){ thisCard.removeClass('topArrow'); var className = 'leftArrow'; var distanceLeft = (distanceLeft)+(39); var thisElTopOffset = (thisElTopOffset)-(150); }else{ thisCard.removeClass('leftArrow'); thisCard.removeClass('bottomArrow').addClass('topArrow'); var distanceLeft = (distanceLeft)-(45); if(thisCard.length && $('.profileCard[data-user='+thisUserID+'] .preloader',cardWrapper).length<1){ $('.profileCard',cardWrapper).hide(); thisCard.addClass(className); rightSide?thisCard.addClass('rightArrow'):thisCard.removeClass('rightArrow'); thisCard.delay(0).css({'top':(thisElTopOffset),'left':distanceLeft,'bottom':thisElbottomoffset}).fadeIn(); } else { var ajaxReturn = ''; //just in case thisCard.remove(); //hover card wrapper markup var rightArrowClass = rightSide?'rightArrow':''; if(thisElTopOffset != "auto"){ topParam = 'px'; if(thisElbottomoffset != "auto"){ bottomparam = 'px'; var profileCardHtml = '