在 MySQL 中存储 PDF 文件

发布于 2024-07-23 07:15:02 字数 448 浏览 4 评论 0原文

如何将 PDF 文档存储在 MySQL 的字段中?

目前,我有一个客户列表,每个客户都有一个包含其帐户信息的证书,他们可以将其提供给其他公司以证明他们是我们的客户。 目前,他们的证书以 PDF 格式导出并通过电子邮件发送给工作人员(客户也会收到纸质副本),而该人员的邮箱中充满了这些电子邮件。 我更愿意将其保存在客户记录中 - 允许通过我们内部 CRM 中的客户文件进行访问。

我考虑过将 PDF 放在一个文件夹中,并将其位置存储为客户记录中的 varchar,但如果 PDF 被移动/删除等。 然后我们就上了一条小溪。

我的理解是,BLOBMEDIUMBLOB 是我用来存储它的字段类型,但我在这方面有点无知。 我不确定如何在字段中存储类似的内容(提供什么 C# 数据类型),以及如何获取它并通过 PDF 阅读器打开它。

How would you store a PDF document in a field in MySQL?

Currently I have a list of customers and each customer has a certificate with information about their account that they can give to other companies to prove that they're our customer. Currently their certificate is exported as a PDF and e-mailed to someone here at work (the customer gets a physical copy as well), and that person's mailbox is filled with these e-mails. I'd much prefer to just have it in the customer's record - allowing it to be accessed via the customer's file in our in-house CRM.

I considered putting the PDFs in a folder and storing their location as a varchar in the customer's record, but if the PDFs get moved/deleted/etc. then we're up a creek.

My understanding is that a BLOB or MEDIUMBLOB is the type of field that I'd use to store it, but I'm a little ignorant in this regard. I'm not sure how to store something like that in the field (what C# datatype to give it), and then how to get it and open it via a PDF reader.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

深海不蓝 2024-07-30 07:15:02

将其放入数据库中,但 BLOB 数据类型可能无法删除它。 MEDIUMBLOB 通常就足够了。

MySQL 数据类型

BLOB, TEXT                L + 2 bytes, where L < 216
MEDIUMBLOB, MEDIUMTEXT    L + 3 bytes, where L < 224
LONGBLOB, LONGTEXT        L + 4 bytes, where L < 232

我已经使用过几次非常好的结果。 请务必保存文件大小,因为这样可以更轻松地检索它。 不确定它是否像 PHP 一样适用于 C#。

如果使用带参数的准备好的语句,数据将自动转义。

另外,我看不出数据库本身在存储此类数据时会变慢的真正原因。 主要瓶颈当然是数据传输。 另外,MySQL 有时会限制查询的最大长度,尤其是响应的最大长度。

一旦你运行它,它就非常整洁,尤其是在处理大量小文件时。 对于少量大文件,这种方法没有意义,最好使用一些备份系统来处理移动/删除的文件。

Put it in the database, but the BLOB datatype probably won't cut it. The MEDIUMBLOB is normally sufficient.

MySQL Datatypes

BLOB, TEXT                L + 2 bytes, where L < 216
MEDIUMBLOB, MEDIUMTEXT    L + 3 bytes, where L < 224
LONGBLOB, LONGTEXT        L + 4 bytes, where L < 232

I've used this several times with very good results. Be sure to save the filesize too, as it makes it easier to retrieve it. Not sure if it applies to C# as it does to PHP.

If using prepared statements with parameters the data will automatically be escaped AFAIK.

Also I can see no real reason as to why the database itself would get slow when storing this type of data in it. The main bottleneck will of course be the transfer of the data. Also MySQL is sometimes restrictive about the maximum length of queries and the responses in particular.

Once you have it running, it's pretty neat, especially when dealing with lots of small files. For a small number of large files, this approach does not make sense, better use some backup system to deal with moved/deleted files.

蓝梦月影 2024-07-30 07:15:02

http://www.phpriot.com/articles/images-in-mysql 是一个很好的教程,有一些背景信息,以及在 MySQL 中存储图像的实现

http://www.phpriot.com/articles/images-in-mysql is a good tutorial with some background information, and an implementation of storing images in MySQL

哆兒滾 2024-07-30 07:15:02

老实说,我认为使用链接而不是实际将文件插入数据库是最好的方法。 这样做会使数据库变得非常慢,并且带来的麻烦比它的价值还要多。

我会将文件上传到指定的文件夹(例如“证书”),并使证书名称与客户编号一起使用,以便轻松查找、编辑、删除等。我见过人们将图像存储在数据库中,但即使这样也是不建议的。

如果您想要的方法绝对是必须的,请查看这篇文章:

http://www.wellho.net/mouth/1001_-pdf-files-upload-via-PHP-store-in-MySQL-retrieve.html

它解释了如何在 mySQL 数据库中存储和检索 .pdf 文件。

祝你好运!

Honestly, I think that going with links instead of actually inserting the file into the database is the best way to go. Doing that will make the database very slow, and will be more trouble than its worth.

I would upload the files to a designated folder like "certificates" and made the certificate names go with a client number so they are easy to find and edit, remove etc. I have seen people store images in databases but even that is advised against.

If the method you wish is definitely a must, check out this article:

http://www.wellho.net/mouth/1001_-pdf-files-upload-via-PHP-store-in-MySQL-retrieve.html

It explains how to store, and retrieve .pdf files in a mySQL Database.

Best of luck!

嘦怹 2024-07-30 07:15:02

我考虑过将 PDF 放入
文件夹并将其位置存储为
客户记录中的 varchar,但是
如果 PDF 被移动/删除等。
然后我们就上了一条小溪。

这就是我会采取的方法。 然后使用一些逻辑,也许是一些 BPEL 类型的东西 - 检测是否有任何文件移动/删除,并向数据库触发触发器以正确更新位置/删除位置

I considered putting the PDFs in a
folder and storing their location as a
varchar in the customer's record, but
if the PDFs get moved/deleted/etc.
then we're up a creek.

That's the approach I would take. Then using some logic perhaps some BPEL type stuff - detect if any of the files move/delete and fire off a trigger to your DB to properly update the location/remove the location

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文