如何在 IIS(Internt Information Services) 的 WebService 中使用 FileStream 上传文件(大尺寸)
我正在使用 C# 创建文件上传服务。 我创建了三种方法:
upload_start(字符串文件名)
upload_continue(字符串文件名,byte[]缓冲区)
upload_end(字符串文件名)
它可以工作,但我不想处理客户端程序中的 3 个函数。如何从客户端程序打开一个FileStream,并让服务器端完成文件上传?
I am using C# to create a service for file uploading.
I created three methods:
upload_start(string filename)
upload_continue(string filename, byte[] buffer)
upload_end(string filename)
It works, but I don't want to handle 3 functions from the client program. How can I open a FileStream from the client program, and let the server side finish the file upload?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最简单的方法可能是使用 wcf 或使用 asp.net 执行类似操作来创建 REST 服务,但您只需执行
POST
操作,服务器就可以完成这项工作。这是一种方法:
http://debugmode.net/2011/05/01/uploading-file-to-server-from-asp-net-client-using-wcf-rest-service/
这将有为您提供更简单的界面。
The simplest approach would probably be to create a REST service, either with wcf or do something similar with asp.net, but you would then just do a
POST
operation, and the server could do the work.Here is one approach:
http://debugmode.net/2011/05/01/uploading-file-to-server-from-asp-net-client-using-wcf-rest-service/
This would have a simpler interface for you.
我认为你不能,因为 FileStream 不可序列化。为什么不向服务传递文件名(就像您已经是的那样)并让服务打开文件并处理它。
I don't think you can as the FileStream is not Serializable. Why not pass the service the file name (as you already are) and have the service open the file and process it.
要在 WebServices(不是 Windows Cominication Foundation WCF)中上传大文件,请遵循以下方法:
在服务器端文件 Web.Config 中添加此 xml 并根据上传的最大大小更改 maxRequestLength 的值,默认 maxRequestLength 为4MB,在本例中8192=MB
在服务器端添加另一个公共函数,该函数接收一个字节[],其中包含文件、文件名和服务器中不会保存文件的路径。
在客户端添加以下代码来发送文件:
For upload a large file in WebServices(not Windows Cominication Foundation WCF) this is the way to follow:
In the server side file Web.Config add this xml and change the value of maxRequestLength depending on the maximum size of upload, by defoult maxRequestLength is 4MB, in this example 8192=MB
Add another public function in server side, this function received a byte[] contains the file, the file name and a path where you wont save the file in the server.
In the client side add this code to send the file: