Forum Discussion
DineshRampal
8 years agoQrew Cadet
Dear all, I could achieve what i wanted .. i.e. importing any kind of file from my system to an Quickbase attachment filed. I used node.js (which i love in addition to my love of quickbase) to write a script and do the needful.
I am attaching a script, if it can be of any help to other fellow community folks. it is yet to be modified for reading all files in a directory which has new / modiiied since last scheduled read of file list in a directory, but that is easy and can be done.
Hope somebody finds it useful and leaves a note.
-- complete Code.---
// test programe to read files for integration with Ispec
.
var http = require('https'); // http request module to push request to quickbase.
var request = require('request'); // npm install request
var fs = require("fs"); // since we want to read files from server
cheerio = require ('cheerio'); // parse XML
console.log("Going to open an existing file");
var bitmap = fs.readFileSync('d:\\MyData\\MyNode.js\\Certificates\\demo.pptx');
// convert binary data to base64 encoded string as quickbase uploads file in base64 only.
uploadfiletoqb(new Buffer(bitmap).toString('base64'));
function uploadfiletoqb(buf1)
{
//*** create authentication ticket.. enable application tokens in your project in qb
var resauth = "";
request({
url : "https://xxxxx.quickbase.com/db/main?a=API_Authenticate&username=youremailaddress&password=yo. password",
method: "POST",
headers: {
"content-type": "application/xml" ,
}
}, function (err, remoteResponse, remotebody){
//console.log("ok "+remoteResponse +" remote body "+remotebody);
auth(remotebody,buf1); // call method to authenticate and create ticket.
if (err) {console.log("error"+remoteResponse+" -- "+err); }
//console.log("error "+remoteResponse +" "+remotebody);
});
}
function auth(auttkt,buf1)
{
var tkt = "";
var $ = cheerio.load(auttkt);
var errordescr = "";
$('qdbapi').each(function(i, element){
var id = $(this);
var childobj = $(this).children().first(); // get the first child
var child = $(this).children().length; // count total no. of childs.
for (i=0;i<child-1;i++)
{
childobj = childobj.next();
var val = childobj.text();
if (childobj[0].name =="ticket")
{
tkt = childobj.text();
//console.log("ticket"+tkt);
}
}
});
// call upload file method to process in quickbase and pass authentication tkt.
upfile(tkt,buf1);
}
function upfile(tkt,buf1)
{
// create xml upload ...see use of apptoken and file is concatenated as a string. var buf1 which is passed from above.
var vxml ='<qdbapi>'+
'<udata>mydata</udata>'+
'<ticket>'+tkt+'</ticket>'+
'<apptoken>enter your app token</apptoken>'+
'<field name="otherfield">Dinesh Rampal</field>'+
'<field name="MyFile" filename="demo.pptx">'+buf1+'</field>'+
'</qdbapi>';
console.log("-----");
console.log(vxml);
console.log("-----");
// create a reqeust to post to quickbase.
request({
url : "https://xxxxx.quickbase.com/db/dbid";,
method: "POST",
headers: {
"content-type": "application/xml" ,
"QUICKBASE-ACTION": "API_AddRecord"
},
body:vxml
}, function (err, remoteResponse, remotebody){
console.log("ok "+remoteResponse +" "+remotebody);
if (err) {console.log("error"+remoteResponse+" -- "+err); }
console.log("error "+remoteResponse +" "+remotebody);
});
}
// create node server.
http.createServer(function (request, response) {
// Send the HTTP header
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-Type': 'text/plain'});
// Send the response body as "Hello World"
response.end('Hello World\n');
}).listen(8081);
// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');
I am attaching a script, if it can be of any help to other fellow community folks. it is yet to be modified for reading all files in a directory which has new / modiiied since last scheduled read of file list in a directory, but that is easy and can be done.
Hope somebody finds it useful and leaves a note.
-- complete Code.---
// test programe to read files for integration with Ispec
.
var http = require('https'); // http request module to push request to quickbase.
var request = require('request'); // npm install request
var fs = require("fs"); // since we want to read files from server
cheerio = require ('cheerio'); // parse XML
console.log("Going to open an existing file");
var bitmap = fs.readFileSync('d:\\MyData\\MyNode.js\\Certificates\\demo.pptx');
// convert binary data to base64 encoded string as quickbase uploads file in base64 only.
uploadfiletoqb(new Buffer(bitmap).toString('base64'));
function uploadfiletoqb(buf1)
{
//*** create authentication ticket.. enable application tokens in your project in qb
var resauth = "";
request({
url : "https://xxxxx.quickbase.com/db/main?a=API_Authenticate&username=youremailaddress&password=yo. password",
method: "POST",
headers: {
"content-type": "application/xml" ,
}
}, function (err, remoteResponse, remotebody){
//console.log("ok "+remoteResponse +" remote body "+remotebody);
auth(remotebody,buf1); // call method to authenticate and create ticket.
if (err) {console.log("error"+remoteResponse+" -- "+err); }
//console.log("error "+remoteResponse +" "+remotebody);
});
}
function auth(auttkt,buf1)
{
var tkt = "";
var $ = cheerio.load(auttkt);
var errordescr = "";
$('qdbapi').each(function(i, element){
var id = $(this);
var childobj = $(this).children().first(); // get the first child
var child = $(this).children().length; // count total no. of childs.
for (i=0;i<child-1;i++)
{
childobj = childobj.next();
var val = childobj.text();
if (childobj[0].name =="ticket")
{
tkt = childobj.text();
//console.log("ticket"+tkt);
}
}
});
// call upload file method to process in quickbase and pass authentication tkt.
upfile(tkt,buf1);
}
function upfile(tkt,buf1)
{
// create xml upload ...see use of apptoken and file is concatenated as a string. var buf1 which is passed from above.
var vxml ='<qdbapi>'+
'<udata>mydata</udata>'+
'<ticket>'+tkt+'</ticket>'+
'<apptoken>enter your app token</apptoken>'+
'<field name="otherfield">Dinesh Rampal</field>'+
'<field name="MyFile" filename="demo.pptx">'+buf1+'</field>'+
'</qdbapi>';
console.log("-----");
console.log(vxml);
console.log("-----");
// create a reqeust to post to quickbase.
request({
url : "https://xxxxx.quickbase.com/db/dbid";,
method: "POST",
headers: {
"content-type": "application/xml" ,
"QUICKBASE-ACTION": "API_AddRecord"
},
body:vxml
}, function (err, remoteResponse, remotebody){
console.log("ok "+remoteResponse +" "+remotebody);
if (err) {console.log("error"+remoteResponse+" -- "+err); }
console.log("error "+remoteResponse +" "+remotebody);
});
}
// create node server.
http.createServer(function (request, response) {
// Send the HTTP header
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-Type': 'text/plain'});
// Send the response body as "Hello World"
response.end('Hello World\n');
}).listen(8081);
// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');