Nisa Khalid
Posts: 0
Joined: Tue May 05, 2015 1:36 pm

Upload local file to database files collection

is it possible to use sql database using this code?

Illya Stepanov
Posts: 0
Joined: Mon Mar 18, 2013 8:48 am

Upload local file to database files collection

Hi Nisa -

The above code is a pure JavaScript client side implementation, it won't work with SQL DB.

Jack Bua
Posts: 0
Joined: Sun Jun 28, 2015 10:16 pm

Upload local file to database files collection

I am trying to do a very similar thing as the OP. Make PDF with jsPDF and save it directly to the server.

I have this so far:

var doc = new jsPDF();
doc.text(20, 20, 'HELLO!');

//Here you can set file name.
var fileName = "myFile.png";
//Here you can use your base64 content. Note: there is no "data:image/jpeg;base64," part.
var base64Content = 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';
var fileBlob = b64toBlob(base64Content, "image/png");

var data = doc; // I AM GUESSING HERE

data.append(fileName, fileBlob);
var onSuccess = function(data){
alert(JSON.stringify(data));
};

create_pdf.execute({
'allowDataModification': false,
'processData': false,
'body': {data: data},
'cache': false,
'success': onSuccess
});

on the button and the rest of the code suggested by Yurii on the BeforeSend event of the service create_pdf (which is file upload service)

I am getting the same initial error as OP:

code: "BCXX002"
description: "Cannot consume content type"

and :

Uncaught ReferenceError: b64toBlob is not defined

I have a feeling I have the right idea but I am missing something crucial.

Any help would be great.

Illya Stepanov
Posts: 0
Joined: Mon Mar 18, 2013 8:48 am

Upload local file to database files collection

Our Server code backend runs on JavaScript V8 engine.

In your part of JavaScript code function b64toBlob(); is not defined.

Jack Bua
Posts: 0
Joined: Sun Jun 28, 2015 10:16 pm

Upload local file to database files collection

Thank you for your reply. unfortunately I do not know what that means. I did notice that error, but I am not sure how to resolve it.

Jack Bua
Posts: 0
Joined: Sun Jun 28, 2015 10:16 pm

Upload local file to database files collection

Let me clarify: I need to take what I do with jsPDF and convert it into something I can save to the database. How can I do that?

Jack Bua
Posts: 0
Joined: Sun Jun 28, 2015 10:16 pm

Upload local file to database files collection

I found a few errors in my understanding of this process, but I still cannot get this to work.

var doc = new jsPDF();
doc.text(20, 20, 'HELLO!');

// Making Data URI
var pdfUriString = doc.output('datauristring');
console.log(pdfUriString);
var pdfOutContainer = jQuery("#pdfOutContainer");
pdfOutContainer.attr("src", pdfUriString);
//Here you can set file name.
var fileName = "test.pdf";
//Here you can use your base64 content. Note: there is no "data:image/jpeg;base64," part.
var base64Content = pdfUriString;
var fileBlob = b64toBlob(base64Content, "application/pdf");
var data = new FormData();
data.append(fileName, fileBlob);
var onSuccess = function(data){
alert(JSON.stringify(data));
};
create_pdf.execute({
'allowDataModification': false,
'processData': false,
'body': {data: data},
'cache': false,
'success': onSuccess

I have the other portion of the code saved as an asset (create new / JS)

I now get:

Uncaught InvalidCharacterError: Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.

Jack Bua
Posts: 0
Joined: Sun Jun 28, 2015 10:16 pm

Upload local file to database files collection

the result of the console.log(pdfUriString) is:

data:application/pdf;base64,JVBERi0xLjMKMyAwIG9iago8PC9UeXBlIC9QYWdlCi9QYXJ...plIDE5Ci9Sb290IDE4IDAgUgovSW5mbyAxNyAwIFIKPj4Kc3RhcnR4cmVmCjE4MzgKJSVFT0Y=

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Upload local file to database files collection

Hi Jack,

Base64 part of your SOURCE is:

pre

JVBERi0xLjMKMyAwIG9iago8PC9UeXBlIC9QYWdlCi9QYXJ...plIDE5Ci9Sb290IDE4IDAgUgovSW5mbyAxNyAwIFIKPj4Kc3RhcnR4cmVmCjE4MzgKJSVFT0Y=

/pre

So you need to cut this part before converting it to blob.

Regards

Jack Bua
Posts: 0
Joined: Sun Jun 28, 2015 10:16 pm

Upload local file to database files collection

Hi Yurii, thanks for your reply.

That worked!

Thank you very much.

Return to “Issues”