如何上传100MB文件?
我正在 ASP.NET 中设计一个文件服务器应用程序。我必须上传一个大小为 100MB 的文件,然后将其保存到 varbinary(max)
中的 SQL Server 2008 数据库中。将文件保存在数据库中是必须的。我正在使用 FileUpload
控件。请建议我如何处理这么大的文件?
更新
任何代码示例都会有很大帮助,任何代码示例吗?
如何读取块中的文件?
I am designing a File Server application in ASP.NET. I have to upload a file of size 100MB and then save it into SQL Server 2008 database in varbinary(max)
. Saving file in DB is must. I am using FileUpload
control. Please suggest me how to handle such large file?
UPDATE
Any code example would be great help, any code sample?
How to read file in chunks?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以通过将特殊的 web.config 放入该目录来更改目录及其子目录的默认限制 (4mb)。这样您就不必让您的整个网站允许大量上传(这样做会使您面临某些类型的攻击)。
这是生产应用程序的示例。这个设置为 200mb:
嵌套 web.configs 不会产生任何负面影响 - 您现有的 web.config 设置仍将在站点范围内工作,并且从该目录开始,这些 httpRuntime 设置将被继承。
在处理上传的页面上,确保您的 ScriptTimeout 高于正常值。这是以秒为单位测量的:
我喜欢在页面上设置它,而不是在 web.config 上,因为它将增加的超时隔离到这一页。
在 SQL 方面,varbinary 列最多可达 2 GB,因此您可以使用该列。
更新:我已经对 50 多个 meg 文件使用了这种方法,但在 100 meg 点,标准方法可能会崩溃。有很多可用的第三方控件可以从那里接管,但我可能会仔细研究 Telerik 的 RadUpload,因为它们是一个高质量的名称,而且看起来非常精致: RadUpload
至于您关于分块读取文件的更新,如果您真的想自己编写,Bobby D 指出了这篇文章: 使用 HttpModule 上传大文件
You can change the default limit (4mb) for a directory and its children by dropping a special web.config into that directory. That way you don't have to make your whole site allow huge uploads (doing so would open you up to a certain kinds of attacks).
Here's an example from a production app. This one is set for 200mb:
Nesting web.configs does not have any negative side-effects -- your existing web.config settings will still work site-wide, and from this directory on down, these httpRuntime settings will be inherited.
On the page that handles the upload, make sure you have a higher than normal ScriptTimeout. This is measured in seconds:
I like setting this on the page rather than web.config because it isolates the increased timeout down to just this one page.
On the SQL side, varbinary columns can be up to 2 gigs so you're good to go there.
Update: I've used this approach with 50+ meg files, but it's possible that at the 100 meg point the standard methods might break down. There are a lot of third party controls available that could take over from there, but I would probably look hard at Telerik's RadUpload since they're a quality name and it looks very polished: RadUpload
As for your update regarding reading the file in chunks, if you really want to write that yourself, Bobby D pointed out this article: Uploading Large Files Using an HttpModule
ASP.NET 文件上传的默认限制为 4MB。这篇文章 描述如何更改默认值并讨论上传大文件的一些问题。
ASP.NET has a default limit of 4MB for file uploads. This article describes how to change that default and discusses some of the issues with uploading large files.
试试这个: 大文件在 ASP.NET 中上传。
如果有帮助请告诉我。
Try this: Large file uploads in ASP.NET.
Let me know if it helps.
几年前(在 ASP 1.1 中?),我使用过这个控件,它非常棒。
看
http://krystalware.com/Products/SlickUpload/
http://krystalware.com/Products/SlickUpload/Documentation/concepts/uploading-sql-server.aspx
Years ago (in ASP 1.1?), I used this control and it was great.
See
http://krystalware.com/Products/SlickUpload/
http://krystalware.com/Products/SlickUpload/Documentation/concepts/uploading-sql-server.aspx
因为您使用的是 SQL Server 2008,
varbinary
列的最大容量为 2GB,所以应该没问题。任何常规上传脚本都可能有效。不过,当您上传这种大小时,底层 HTTP 或 TCP/IP 连接可能会成为问题,但对此您确实无能为力。Because you are using SQL Server 2008 with its 2gb maximum for
varbinary
columns, you should be fine. Any regular upload script will probably work. When you get into uploads of that size though, underlying HTTP or TCP/IP connections can become a problem, but there really isn't anything you can do about that.将其分成多个部分,为每个部分分配一个序列号,服务器端接收到文件后(假设数据库在那里)收集,根据序列号对各个部分进行排序,并通过剥离序列号重新加入它们。希望您不会超时,如果这是唯一的问题!也许您可以使用 TPL 上传多个部分并以相同的方式重新加入它们!
Break it into parts, assign a sequence number to each part, upon receiving the file on the server side (assuming DB is there) collect, sort the parts according to the sequence number, and rejoin them by stripping off the sequence numbers. Hopefully you wont get timeout, if that is the only issue! Probably you could your TPL for uploads of the multiple parts and rejoin them the same way!