传输后需要从服务器删除的大文件的WriteFile与TransmitFile [英] WriteFile vs TransmitFile for large files that need to be deleted from the server after transfer

查看:81
本文介绍了传输后需要从服务器删除的大文件的WriteFile与TransmitFile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须触发用户将大文件下载到Web浏览器,然后在其中创建要在服务器上传输的文件,然后立即将其删除.我找到了足够的示例,发现我可能应该使用Response.TransmitFile或Response.WriteFile ...,但是听说这两者都存在问题:

I have to trigger user downloads of large files to a webbrowser, where I create the file to transfer on the server, then delete it immediately afterwards. I've found enough examples to see that I should probably use Response.TransmitFile or Response.WriteFile... but have heard there are problems with both:

WriteFile是同步的,但是它在将文件发送给用户之前将其缓冲在内存中.由于我要处理的文件非常大,因此可能会导致问题.

WriteFile is synchronous, but it buffers the file in memory before sending it to the user. Since I'm dealing with very large files, this could cause problems.

TransmitFile不会在本地缓冲,因此它适用于大型文件,但它是异步的,因此调用TransmitFile后无法删除文件.显然刷新文件并不能保证我也可以删除它?

TransmitFile doesn't buffer locally so it does work for large files, but it is asynchronous, so I can't delete the file after calling TransmitFile. Apparently flushing the file doesn't guarantee that I can delete it either?

处理此问题的最佳方法是什么?

What is the best way of dealing with this?

还有BinaryWrite ...我可以遍历文件流,将其分段复制吗?

There is the BinaryWrite also... could I loop through a file stream, copying it in segments?

推荐答案

这是一个使用TransmitFile的很好的解决方案,但是一旦使用委托完成,您就可以执行以下操作:

Here's a good solution which uses TransmitFile but allows you to do something once it's done using a delegate:

http://improve.dk/blog/2008/03/29/response-transmitfile-close-will-kill-your-application

只需删除文件末尾的日志记录即可.

Just replace the logging at the end with file deletion.

这篇关于传输后需要从服务器删除的大文件的WriteFile与TransmitFile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆