概要
本分步指南介绍如何使用 Microsoft ASP.NET 和 Microsoft Visual C# .NET 建立 Web 服务和 Web 服务客户端来发送和接收二进制文档。您可以使用 ASP.NET 和 Visual C# .NET 建立一项 Web 服务,用于将二进制文档保存到 Web 服务器上的文件夹和从 Web 服务器上的文件夹中检索二进制文档。可以使用此服务作为 Web 上的一个简单“文档管理系统”。返回页首
建立 Web 服务
- 在 Microsoft Visual Studio .NET 中的文件菜单上,单击新建,然后单击项目。
- 在 Visual C# 项目中,选择 ASP.NET Web 服务。为位置键入或粘贴 http://localhost/DocumentManagementService,然后单击确定。默认情况下,会创建 Service1.asmx 并将其显示在设计视图中。
- 在视图菜单上,单击代码以显示 Service1.asmx 的代码视图。
- 将以下 WebMethods 代码添加到 Service1 类:
[WebMethod] public bool SaveDocument( Byte[] docbinaryarray, string docname) { string strdocPath; strdocPath = "C://DocumentDirectory//" + docname; FileStream objfilestream =new FileStream(strdocPath,FileMode.Create,FileAccess.ReadWrite); objfilestream.Write(docbinaryarray,0,docbinaryarray.Length); objfilestream.Close(); return true; } [WebMethod] public int GetDocumentLen(string DocumentName) { string strdocPath; strdocPath = "C://DocumentDirectory//" + DocumentName; FileStream objfilestream = new FileStream(strdocPath,FileMode.Open,FileAccess.Read); int len = (int)objfilestream.Length; objfilestream.Close(); return len; } [WebMethod] public Byte[] GetDocument(string DocumentName) { string strdocPath; strdocPath = "C://DocumentDirectory//" + DocumentName; FileStream objfilestream = new FileStream(strdocPath,FileMode.Open,FileAccess.Read); int len = (int)objfilestream.Length; Byte[] documentcontents = new Byte[len]; objfilestream.Read(documentcontents,0,len); objfilestream.Close(); return documentcontents; }
注意:以上代码将文档保存到服务器上的 <根目录>://DocumentDirectory// 目录路径中。将该路径更改为 Web 服务器上要在其中保存文档的文件夹。
- 将以下命名空间添加到 Service1.asmx 的开头:
using System.IO;
- 测试 Web 服务:
- 在调试菜单上,单击开始以启动该 Web 服务。这将启动 Web 浏览器,并显示包含服务说明的“帮助”页面。
- 确保显示了 SaveDocument、GetDocument 和 GetDocumentLen 方法。
- 关闭 Web 浏览器窗口以停止调试。
为 Web 服务建立客户端
- 在 Visual Studio .NET 中的文件菜单上,单击添加项目,然后单击新建项目。
- 在 Visual C# 项目列表中,选择 Windows 应用程序,然后单击确定。默认情况下会创建 Form1。
- 添加对该 Web 服务的 Web 引用,如下所示:
- 在解决方案资源管理器中,右键单击该客户端项目项。然后在上下文菜单上选择添加 Web 引用。
- 在添加 Web 引用对话框中,键入指向该 Web 服务的 Web 服务描述语言 (WSDL) 文件的 URL,然后按 Enter 键。
注意:WSDL 文件的默认位置是 http://localhost/DocumentManagementService/Service1.asmx?WSDL。 - 在添加 Web 引用对话框中,单击添加引用。
- 将两个按钮添加到 Form1。 将 button1 的文本属性设置为“在服务器上存储文档”。 将 button2 的文本属性设置为“从服务器检索文档”。
- 双击 button1 和 button2,为按钮创建默认的 Click 事件处理程序。
- 使用下列代码替换这些处理程序:
string sFile = "<file path>"; private void button1_Click(object sender, System.EventArgs e) { FileStream objfilestream = new FileStream(sFile,FileMode.Open,FileAccess.Read); int len = (int)objfilestream.Length; Byte[] mybytearray = new Byte[len]; objfilestream.Read(mybytearray,0,len); localhost.Service1 myservice = new localhost.Service1(); myservice.SaveDocument(mybytearray,sFile.Remove(0,sFile.LastIndexOf("//")+1)); objfilestream.Close(); } private void button2_Click(object sender, System.EventArgs e) { MemoryStream objstreaminput = new MemoryStream(); FileStream objfilestream = new FileStream(sFile.Insert(sFile.LastIndexOf("."),"2"), FileMode.Create,FileAccess.ReadWrite); localhost.Service1 myservice = new localhost.Service1(); int len = (int)myservice.GetDocumentLen(sFile.Remove(0,sFile.LastIndexOf("//")+1)); Byte[] mybytearray = new Byte[len]; mybytearray = myservice.GetDocument(sFile.Remove(0,sFile.LastIndexOf("//")+1)); objfilestream.Write(mybytearray,0,len); objfilestream.Close(); }注意:sFile 变量必须包含要上载到服务器的文档的本地文件路径。文档在下载后将被放入同一文件夹中,并将值 2 附加到文件名后。
- 将以下命名空间添加到文件的开头:
using System.IO;
- 在解决方案资源管理器中,右键单击该客户端项目项。然后在上下文菜单上选择“设为启动项目”。
试运行
- 在调试菜单上,单击开始。将出现 Form1。
- 单击标记为“在服务器上存储文档”的按钮。这将调用 SaveDocument Web 方法。此 Web 方法将本地文档存储在服务器上的 <根目录>:/DocumentDirectory/ 文件夹中。传送文档后,确认目标文件夹中是否存在该文件。
- 单击标记为“从服务器检索文档”的按钮。这将调用 GetDocument Web 方法。此 Web 方法从服务器上的 <根目录>:/DocumentDirectory/ 文件夹中检索文档。文档将保存在代码中指定的本地驱动器上。
这篇文章中的信息适用于:
- Microsoft ASP.NET (included with the .NET Framework) 1.0
- Microsoft Visual C# .NET (2002)
| 最近更新: | 2004-6-16 (4.0) |
| 关键字: | kbHOWTOmaster KB318425 kbAudDeveloper |

1898

被折叠的 条评论
为什么被折叠?



