使用指针将大文件顺序上传到数据库
我的应用程序要求用户可以将文件上传到数据库。此时,用户上传文件,网页将其保存在临时目录中,然后逻辑将文件加载到 Byte[] 中,并将该数组作为参数传递给“insert”SQL 语句。
这种方法的问题在于,如果用户尝试上传 1GB 文件,则会导致服务器占用 1GB 内存来将该文件存储为 Byte[],并且该内存稍后应该被 GCollection。如果多个用户同时这样做,服务器可能会崩溃。
避免这种情况的一种方法是限制文件的大小......但客户不想这样做。因此,最好的方法似乎是使用指针将文件顺序上传到数据库。我找到了 SQL Server 使用名为 updatetext 的特殊函数的示例< /a>,但我想知道一种对所有类型的数据库都有效的方法,即。了解是否可能以及如何将文件分块上传到数据库。
干杯。
It's a requirement in my app that the users can upload files to the database. At this point, the user upload the file and the webpage save it in a temp directory, secondly the logic load the file in a Byte[] and pass this array as parameter to the "insert" SQL statement.
The problem with that approach, is that if the user try to upload a 1GB file, it'll cause the server takes 1GB of memory in order to store that file as Byte[], and that memory should be GCollected later on. If several users do that at the same time, the server could collapse.
One way to avoid this, is limit the size of the file... but the customer don't want to do that. So the best approach seems to be upload the file to the database sequentially using a pointer. I've found an example of this for SQL Server using a special function named updatetext, but I'd like to know an approach valid for all kinds of databases, ie. to know if is possible and how to upload a file to a database in chunks.
Cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不相信这样的事情是可能的。
更好的选择可能只是将文件存储在数据库服务器上的某个路径中,并仅将该文件的路径存储在数据库中。这将根本不需要将文件加载到内存中。您可以使用 FTP 或简单地将文件复制到数据库服务器,具体取决于网络配置。
I don't believe such a thing is possible.
A better option may simply be to store the file in a path on the database server and store only the path to that file in the database. This will eliminate the need for loading the file into memory at all. You can either use FTP or simply copy the file to the database server, depending on the network configuration.
正如其他人指出的那样,我也相信将文件存储在文件系统上是更好的选择。请注意,您可以拥有可配置的共享文件夹(或文件服务器),以便轻松移动应用程序。
无论如何,如果您必须将大型二进制数据分块写入数据库,那么不同数据库的方式会有所不同。您需要检查该数据库的 ADO.NET 数据提供程序是否支持这种支持。这是文章,解释了如何阅读和使用为 Sql Server 和 Oracle 以块的形式写入大数据。由于方式会根据数据库或托管提供程序而变化,因此您必须抽象 API(作为某些接口)并针对接口编写代码。然后创建数据库特定的实现并使用工厂或IOC等来选择实现。
As others have pointed out, I also believe that storing file on file system is better option. Note that you can have configurable shared folder (or file server) that can allow easy moving of the application.
Regardless, if you must write large binary data to database in chunks then ways will differ across databases. And you need to check for such support in ADO.NET data provider for that database. Here's the article that explain how to read & write large data in chunks for Sql Server and Oracle. As the way would changes as per database or managed provider, you have to abstract your API (as some interface) and code against the interface. Then create database specific implementation and use factory or IOC etc to choose the implementation.