Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

coldfusion How to save pdf file in database and fetch it in browser

Insert to MS-SQL 2008 Database

<cffile action="READBINARY" file="#form.FileContents#" variable="binPDF">
<cfquery name="Uploaded" datasource="#cfbasedatasource#">
INSERT INTO UploadedFiles
    (
    AccountId,
    Filecontent
    )
VALUES
    (
    '#UrlAccountId#',
    <cfqueryparam value='#binPDF#' cfsqltype='cf_sql_blob'>
    )
</cfquery>

========================= This is Show from Database to browser:

<cfset binaryData = #cfGetfilecontentquery.Filecontent[1]#>
<cfheader name="Content-Disposition" value="inline; filename=testDocument.pdf">
<cfcontent variable="#binarydata#" type="application/pdf" reset="yes" />

But I can only see in browser "Fail to Load PDF Document"

like image 363
Sung Ham Avatar asked Oct 15 '25 05:10

Sung Ham


1 Answers

Your code is correct in principle. Make sure that the settings in the CF admin allow for blob data - otherwise it may be truncated in the DB. It's pretty typical to not allow BLOB data through the driver (by default it is NOT allowed in the DSN settings). If you enable it you should be good.


I'm posting this answer so you can flag it as correct based on the comments (per Leigh's suggestion). :)

like image 58
Mark A Kruger Avatar answered Oct 17 '25 20:10

Mark A Kruger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!