Reputation: 19
I am currently working on a windows form application and I need to create functionality that enables users to upload PDF files to MySQL, and also be able to read the PDF file from the MySQL database then display the PDF file in the window form.
Can anyone show me some example code for how to do this?
Upvotes: 1
Views: 7972
Reputation: 1081
You can store the pdf in a mysql database inside a column (varbinary(MAX)) with a byte[], to build byte[] try this:
byte[] bytes = null;
try
{
bytes = File.ReadAllBytes(fileName);
}
catch (IOException)
{
...
}
filename is the pdf name with the path. After that build some sql query to insert the byte[] and to get the byte[]
To show the pdf you need to convert byte[] to file like this:
Directory.CreateDirectory(Path.GetDirectoryName(fileName));
using (Stream file = File.Create(fileName))
{
file.Write(buffer, 0, buffer.Length);
}
buffer is the byte[]
then finally if you want to open the pdf:
Process process = new Process();
process.StartInfo.FileName = path;
process.Start();
path is the pdf name with the path.
Upvotes: 1