0

I have uploaded a pdf file (varbinary(MAX)) in MSSQL running on my desktop via VS2010 (Ref) with just the UPDATE statement in the Ref.

The table has additional data associated with the pdf, like name, description, date.

Table in database:

Name       |  Description    |    Date      |    File            |

DataSheet  |  Milling M/C    |  2004-01-01  |    <Binary data>   |

Is it possible to display the table content in a web page as

Name       |  Description    |    Date      |    File            |`

DataSheet  |  Milling M/C    |  2004-01-01  | (link to the pdf)  |

or

Name       |  Description    |    Date      |    File            |

DataSheet  |  Milling M/C    |  2004-01-01  | (an icon of pdf)   |

using PHP so that user can click the link/file to view it. There will be lot of rows, I just gave 1 row for example.

Please let me know if I am not clear, thanks in advance.

PS: I did create some images but was not able to post them, sorry about the format


What I have done so far


I am using 'Connect without a DSN (using a connection string)' from here

$conn = new COM ("ADODB.Connection") or die("Cannot start ADO");

$connStr = "PROVIDER=SQLOLEDB;SERVER=".$myServer.";UID=".$myUser.";PWD=".$myPass.";DATABASE=".$myDB;

$conn->open($connStr);

$query = "select * from Table";

$rs = $conn->execute($query);

$num_columns = $rs->Fields->Count();

for ($i=0; $i < $num_columns; $i++) {
$fld[$i] = $rs->Fields($i);
    }

while (!$rs->EOF)
{
for ($i=0; $i < $num_columns; $i++) {
echo $fld[$i]->value;
    }   
$rs->MoveNext();    
}

The result I am getting is garbage for binary data.

DataSheet | Milling M/C | 2004-01-01 | some_garbage |

1 Answer 1

1

Yes it is possible. Create a link to a separate page that accepts a record is in the query string. For instance, you can link to ViewFile.php?recordid=123.

Then, in ViewFile.php, you can get the binary data, output some headers, and output the binary (I think even echo would suffice for that).

The headers should contain at least a Content-Type header, telling the browser that the binary data is to be interpreted as application/pdf data. If it is not a pdf, you should specify appropriate headers. Lists of valid Content-Types can be found all over the internet.

You can specify a filename too. But the important thing is that you can determine what kind of data it is. If you don't know whether it is an icon or a pdf, you cannot tell the browser either. The browser will need to know (by reading the content-type header) how the data should be interpreted. It cannot guess it, even when the url would have a .pdf extension.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the answer, I am very new to all the stuff. I will try and let you know what I did. Right now, below is what I have so far.
I was not able to get the formatting I want, so will edit my question or post as answer.
@GolezTrol , this time I followed 'Connect with a DSN'. $query=<query to select just one <Binary Data>; $result=odbc_exec($conn, $query); $outputPdf=odbc_result($result, 1); header("Content-type: application/pdf"); echo $outputPdf; and the output is still garbage. Am I doing something wrong, please advise, thanks again.
Hmm I found an example that does just about that: wellho.net/resources/ex.php4?item=h110/pdfget.php But it could be an encoding problem. I'm not sure...
Is there any way you can check if the php file contains valid data? Mayby you can save the data to a file, both from PHP and from PHPMyAdmin or whichever tool you are using. If that does work, then it's in transporting the data through PHP. If that doesn't give you a good PDF file, the data in the database itself may be corrupt.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.