您的位置:首页 > 编程语言 > ASP

如何使用 ASP.NET Web 服务和 Visual C# .NET 发送和接收二进制文档

2008-07-10 09:47 1216 查看


概要




建立 Web 服务




为 Web 服务建立客户端




试运行




参考


var sectionFilter = "type != 'notice' && type != 'securedata' && type != 'querywords'";
var tocArrow = "/library/images/support/kbgraphics/public/en-us/downarrow.gif";
var depthLimit = 10;
var depth3Limit = 10;
var depth4Limit = 5;
var depth5Limit = 3;
var tocEntryMinimum = 1;

概要

loadTOCNode(1, 'summary');
本分步指南介绍如何使用 Microsoft ASP.NET 和 Microsoft Visual C# .NET 建立 Web 服务和 Web 服务客户端来发送和接收二进制文档。您可以使用 ASP.NET 和 Visual C# .NET 建立一项 Web 服务,用于将二进制文档保存到 Web 服务器上的文件夹和从 Web 服务器上的文件夹中检索二进制文档。可以使用此服务作为 Web 上的一个简单“文档管理系统”。

建立 Web 服务

loadTOCNode(2, 'summary');
1.在 Microsoft Visual Studio .NET 中的文件菜单上,单击新建,然后单击项目
2.Visual C# 项目中,选择 ASP.NET Web 服务。为位置键入或粘贴 http://localhost/DocumentManagementService,然后单击确定。默认情况下,会创建 Service1.asmx 并将其显示在设计视图中。
3.视图菜单上,单击代码以显示 Service1.asmx 的代码视图。
4.将以下 WebMethods 代码添加到 Service1 类
[code][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;
}

[/code]
注意:以上代码将文档保存到服务器上的 <根目录>://DocumentDirectory// 目录路径中。将该路径更改为 Web 服务器上要在其中保存文档的文件夹。

5.将以下命名空间添加到 Service1.asmx 的开头:
[code]using System.IO;

[/code]
6.测试 Web 服务:
a. 调试菜单上,单击开始以启动该 Web 服务。这将启动 Web 浏览器,并显示包含服务说明的“帮助”页面。
b. 确保显示了 SaveDocumentGetDocumentGetDocumentLen 方法。
c. 关闭 Web 浏览器窗口以停止调试。
为 Web 服务建立客户端
loadTOCNode(2, 'summary');
1.在 Visual Studio .NET 中的文件菜单上,单击添加项目,然后单击新建项目
2.Visual C# 项目列表中,选择 Windows 应用程序,然后单击确定。默认情况下会创建 Form1。
3.添加对该 Web 服务的 Web 引用,如下所示:
a. 在解决方案资源管理器中,右键单击该客户端项目项。然后在上下文菜单上选择添加 Web 引用
b. 添加 Web 引用对话框中,键入指向该 Web 服务的 Web 服务描述语言 (WSDL) 文件的 URL,然后按 Enter 键。

注意:WSDL 文件的默认位置是 http://localhost/DocumentManagementService/Service1.asmx?WSDL。
c. 添加 Web 引用对话框中,单击添加引用
4.将两个按钮添加到 Form1。 将 button1 的文本属性设置为“在服务器上存储文档”。 将 button2 的文本属性设置为“从服务器检索文档”。
5.双击 button1button2,为按钮创建默认的 Click 事件处理程序。
6.使用下列代码替换这些处理程序:
[code]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();
}

[/code]注意:sFile 变量必须包含要上载到服务器的文档的本地文件路径。文档在下载后将被放入同一文件夹中,并将值 2 附加到文件名后。

7.将以下命名空间添加到文件的开头:
[code]using System.IO;

[/code]
8.在解决方案资源管理器中,右键单击该客户端项目项。然后在上下文菜单上选择“设为启动项目”。

试运行

loadTOCNode(2, 'summary');
1.调试菜单上,单击开始。将出现 Form1。
2.单击标记为“在服务器上存储文档”的按钮。这将调用 SaveDocument Web 方法。此 Web 方法将本地文档存储在服务器上的 <根目录>:/DocumentDirectory/ 文件夹中。传送文档后,确认目标文件夹中是否存在该文件。
3.单击标记为“从服务器检索文档”的按钮。这将调用 GetDocument Web 方法。此 Web 方法从服务器上的 <根目录>:/DocumentDirectory/ 文件夹中检索文档。文档将保存在代码中指定的本地驱动器上。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐