使用 C# 通过网络发送文件的最佳方式是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/95235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 11:39:25  来源:igfitidea点击:

What's the best way to send a file over a network using C#?

提问by screenglow

Can anyone point me to a tutorial on the best way to open a connection from client to server, read in a binary file and send its contents reliably across the network connection? Even better, is there an open source library that already does this that I could refer to?

任何人都可以向我指出有关打开从客户端到服务器的连接、读取二进制文件并通过网络连接可靠地发送其内容的最佳方法的教程吗?更好的是,是否有一个开源库已经做到了这一点,我可以参考?

采纳答案by GEOCHET

You should look into binary serialization and sending it over a TCP socket.

您应该研究二进制序列化并通过 TCP 套接字发送它。

Good explanation on different types of serialization:

对不同类型的序列化的很好的解释:

http://www.dotnetspider.com/resources/408-XML-serialization-Binary-serialization.aspx

http://www.dotnetspider.com/resources/408-XML-serialization-Binary-serialization.aspx

Good primer on TCP Client/Server in C#:

C# 中 TCP 客户端/服务器的良好入门:

http://www.codeproject.com/KB/IP/tcpclientserver.aspx

http://www.codeproject.com/KB/IP/tcpclientserver.aspx

回答by Alex Fort

How about using HTTP or FTP? They were sort of made for this.

使用 HTTP 或 FTP 怎么样?它们是为此而生的。

Alex

亚历克斯

回答by rjzii

Depending upon where you are sending the file to, you might want to take a look at WebClient.UploadFileAsyncand WebClient.UploadFile.

根据您将文件发送到的位置,您可能需要查看WebClient.UploadFileAsyncWebClient.UploadFile

回答by Guille

I wouldn't use HTTP or FTP, for a single file it's too much overhead and too much to code, especially having a simple TCP server almost already made for you in C#.

我不会使用 HTTP 或 FTP,对于单个文件来说,它的开销太大,编码太多,尤其是有一个几乎已经用 C# 为您制作的简单 TCP 服务器。

回答by typemismatch

This depends what you mean by network - if you're copying on a local network you can just use the file copy operations inside System.IO. If you're wanting to send to remote servers I do this using web services. I compress byte arrays and send them over and decompress on the remote side. The byte array is super easy to write back to disk using streams.

这取决于您所说的网络是什么意思 - 如果您在本地网络上进行复制,则可以仅使用 System.IO 中的文件复制操作。如果您想发送到远程服务器,我会使用 Web 服务来执行此操作。我压缩字节数组并将它们发送过来并在远程端解压缩。使用流将字节数组写回磁盘非常容易。

I know some people prefer base 64 strings instead of the byte[]. not sure if it matters.

我知道有些人更喜欢 base 64 字符串而不是 byte[]。不确定是否重要。

回答by Jason Olson

Sockets may be the best route if you're just having to do it over the network. If you use TCP, you get the reliability of communication but take an impact on speed. If you need higher performance, you could try using UDP instead. But the downside to UDP is that packet delivery and order is not guaranteed, so you would need to write all that plumbing yourself.

如果您只需要通过网络进行操作,套接字可能是最佳途径。如果您使用 TCP,您可以获得通信的可靠性,但会影响速度。如果您需要更高的性能,您可以尝试使用 UDP。但是 UDP 的缺点是无法保证数据包的传递和顺序,因此您需要自己编写所有管道。

If you are needing to transfer files over the web itself (programatically, and if you can't use FTP), then a web service approach via MTOMmight fit your needs.

如果您需要通过 Web 本身传输文件(以编程方式,并且如果您不能使用 FTP),那么通过MTOM的 Web 服务方法可能适合您的需求。

If you are building on top of Windows Server 2003 R2, Windows Vista, or Windows Server 2008 and doing internal network transfers, another option is to leverage the new Remote Differential Compressionfeature. This not only does a really good job at compressing a file to minimize network traffic, but is also used directly by DFS replication. Downside (as a .NET developer), it's a COM+ technology.

如果您在 Windows Server 2003 R2、Windows Vista 或 Windows Server 2008 之上构建并进行内部网络传输,则另一种选择是利用新的远程差分压缩功能。这不仅在压缩文件以最大限度地减少网络流量方面做得非常好,而且还直接由 DFS 复制使用。缺点(作为 .NET 开发人员),它是一种 COM+ 技术。