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

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

概要
建立 Web 服务
1. 在 Microsoft Visual Studio .NET 中的文件菜单上,单击新建,然后单击项目。
2. 在 Visual C# 项目中,选择 ASP.NET Web 服务。为位置键入或粘贴 http://localhost/DocumentManagementService,然后单击确定。
默认情况下,会创建 Service1.asmx 并将其显示在设计视图中。
3. 在视图菜单上,单击代码以显示 Service1.asmx 的代码视图。
4. 将以下 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 服务器上要在其中保存文档的文件夹。

 
5. 将以下命名空间添加到 Service1.asmx 的开头:using System.IO;
 
6. 测试 Web 服务:a.  在调试菜单上,单击开始以启动该 Web 服务。这将启动 Web 浏览器,
并显示包含服务说明的“帮助”页面。
b.  确保显示了 SaveDocument、GetDocument 和 GetDocumentLen 方法。
c.  关闭 Web 浏览器窗口以停止调试。
 
返回页首
为 Web 服务建立客户端
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. 双击 button1 和 button2,为按钮创建默认的 Click 事件处理程序。
6. 使用下列代码替换这些处理程序: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 附加到文件名后。

 
7. 将以下命名空间添加到文件的开头:using System.IO;
 
8. 在解决方案资源管理器中,右键单击该客户端项目项。然后在上下文菜单上选择“设为启动项目”。
返回页首
试运行
1. 在调试菜单上,单击开始。将出现 Form1。
2. 单击标记为“在服务器上存储文档”的按钮。这将调用 SaveDocument Web 方法。
此 Web 方法将本地文档存储在服务器上的 <根目录>:/DocumentDirectory/ 文件夹中。
传送文档后,确认目标文件夹中是否存在该文件。
3. 单击标记为“从服务器检索文档”的按钮。这将调用 GetDocument Web 方法。
此 Web 方法从服务器上的 <根目录>:/DocumentDirectory/ 文件夹中检索文档。
文档将保存在代码中指定的本地驱动器上。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值