如何避免TIdHTTP组件在使用Get时一次性分配所有文件空间?
我正在使用 TIdHTTP.Get
下载文件。作为参数,我传递了一个 TFileStream 对象,我注意到当我执行 get 方法时,文件大小会在下载完成之前立即分配,我的问题是我如何才能避免 TIdHTTP 组件在使用 Get 时一次分配磁盘中的所有文件大小?
I'm using TIdHTTP.Get
to download a file. As parameter I'm passing a TFileStream
object, I have noticed that when i execute the get method the file size is allocated at once before which the download was completed, my question is How I can avoid which the TIdHTTP component allocate all the file size in the disk at once when use Get?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如雷米在评论中所说,如果提前知道响应大小(来自 Content-Length 标头成员),Indy 将始终预分配整个流大小。
因此,我想到的避免磁盘空间分配的唯一解决方法是使用内存流并将数据从该流复制到正在进行的文件中。
在磁盘上预先分配整个下载文件的大小非常有用,因为即使您将文件下载到内存流中,您仍然需要硬盘驱动器上有足够的空间来保存它(当然还有足够的可用内存)用于下载本身)。使用内存流的一个好处是您可以获得释放一些磁盘空间的时间,但这在很大程度上取决于您要做什么。
As Remy said in his comment, if the response size is known in advance (from the Content-Length header member), Indy will always pre-allocate the entire stream size.
So the only workaround how to avoid the disk space allocation that came to my mind is to use the memory stream and copy the data from that stream into the file in progress.
The pre-allocation of the entire downloaded file size on disk is useful because even if you download a file into the memory stream then you'll still need to have enough space on your hard drive to save it anyway (and of course enough free memory for the download itself). A benefit of using memory stream is that you can get the time for freeing some disk space, but it depends a lot on what you are going to do.