在 C# 中通过网络发送大文件的好方法?
我正在尝试构建一个应用程序,该应用程序可以从网络中另一台计算机上运行的服务请求文件。 这些文件可能相当大(有时 500mb 以上)。 我正在考虑通过 TCP 发送它,但我担心它可能需要将整个文件存储在内存中。
可能只有一个客户。 复制到共享目录也是不可接受的。 唯一需要的通信是客户端说“gimme xyz”,然后服务器发送它(以及确保正确发生的一切)。
有什么建议么?
I am trying to build an application that can request files from a service running on another machine in the network. These files can be fairly large (500mb + at times). I was looking into sending it via TCP but I'm worried that it may require that the entire file be stored in memory.
There will probably only be one client. Copying to a shared directory isn't acceptable either. The only communication required is for the client to say "gimme xyz" and the server to send it (and whatever it takes to ensure this happens correctly).
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
这是一个更简单的方法。 使用 BITS(后台智能传输服务)。 它已经内置于 WinXP 和 Vista 中。 它基本上是驱动 Windows 更新的动力。
http://blogs. msdn.com/powershell/archive/2009/01/11/transferring-large-files-using-bits.aspx
http://blogs.msdn.com/jamesfi/archive/2006/12/23/how-to -use-bits-to-transfer-files.aspx
这是某人编写的一个很好的托管 BITS 包装器以及如何使用它。
http://www.codeproject.com/KB/cs/Managed_BITS.aspx
Here is an easier way. Using BITS (Background Intelligent Transfer Service). Its already built into WinXP and Vista. Its basically what drives Windows Updates.
http://blogs.msdn.com/powershell/archive/2009/01/11/transferring-large-files-using-bits.aspx
http://blogs.msdn.com/jamesfi/archive/2006/12/23/how-to-use-bits-to-transfer-files.aspx
Here is a nice managed BITS wrapper someone wrote and how to use it.
http://www.codeproject.com/KB/cs/Managed_BITS.aspx
您可以在.NET 中使用套接字来传输文件和数据。
You can use sockets in .NET to transfer files and data.
您可能需要考虑WCF 流。
You might want to consider WCF streaming.
这篇文章可能会对您有所帮助。 它是关于在 .NET 中发送大文件。
检查链接:
http://codetechnic.blogspot。 com/2009/02/sending-large-files-over-tcpip.html
This article may help you. It is about sending large files in .NET.
Check the link:
http://codetechnic.blogspot.com/2009/02/sending-large-files-over-tcpip.html
小心 BITS。 这是一个非常好的协议,但不是 Windows 更新程序的关键部分。 我们发现几乎没有任何企业客户允许将 BITS 更新到他们的计算机上; 因此我们无法构建依赖它的应用程序。
Be careful with BITS. It is a very good protocol but not a critical part of the windows update program. We found that hardly any of our corporate clients allowed the BITS update onto their machines; therefore we couldn't build an app that relied on it.
如果 FTP 是一种选择,那么为了简单起见我会选择它。 否则您就会进入 TCP/IP 套接字编程的世界。
If FTP was an option then I'd go with that for the sake of simplicity. Otherwise you're into a world of TCP/IP socket programming.
通过开源 edtFTPnet 库使用 FTP。 快速而简单。
Use FTP via the open source edtFTPnet library. Fast and simple.
使用TransmitFile(这是一个Win32 函数;也许它也是.NET 库的一个方法)。
Use
TransmitFile
(which is a Win32 function; perhaps it's a method of the .NET library as well).如果文件物理存在于计算机上,为什么不将它们放在一个文件夹中,使该文件夹成为 IIS 中的虚拟目录,并使用基于内容的路由和/或 URL 重写将请求路由到它们。
If files exist physically on the machine why not just put them in a folder, make that folder a virtual directory in IIS, and use Content-Based Routing and/or URL Rewriting to route the requests to them.
就我个人而言,我会寻求能够平衡速度、可靠性和经济代码的东西,因此我会将其基于 TCP 网络流。 代码的客户端将如下所示:
然后有一个服务器类来提供该文件。 请注意,整个文件并未加载到内存中,而是从
FileStream
中分块读取。TcpClientWrapper 几乎是带有 System.Net.Sockets.TcpClient 对象和底层 NetworkStream 对象的样板代码。 我实际上也不需要发布这个,但只是为了给出一些指示,它们的构造将包含类似这样的内容:
并且
DataReceivedAsync
方法是样板套接字数据处理,并且会引发一个事件 o 共享接收到的数据返回给消费者(在本例中为客户端):将数据从包装器发送回客户端的事件:
Personally I'd go for something that balances speed, reliability and economical code, so I'd base it on a TCP network stream. The client-side of the code would look like this:
Then have a server class to serve the file. Note how the whole file isn't loaded into memory, instead it is read in chunks from a
FileStream
.The TcpClientWrapper is pretty much boiler plate code with the
System.Net.Sockets.TcpClient
object and the underlyingNetworkStream
object. I don't really need to post this as well, but just to give some pointers ther construction would contain something like this:and the
DataReceivedAsync
method is boilerplate socket data handling and would raise an event o share the received data back to the consumer (the client in this case):The event to ship data from the wrapper back to the client: