二:通过Web Services上载文件
向服务器上载文件可能有许多种方法,在利用Web Services上载文件的方法中,下面的这个方法应该是最简单的了。我们仍象前面的例子那样,首先建立Upload.asmx文件,其Upload.asmx.cs内容如下,里面已经做了注释:
|
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Web; using System.Web.Services; using System.IO;
namespace xml.sz.luohuedu.net.aspxWebCS { /// <summary> /// Upload 的摘要说明。 /// </summary> [WebService(Namespace="http://xml.sz.luohuedu.net/", Description="在Web Services里利用.NET框架进上载文件。")] public class Upload : System.Web.Services.WebService { public Upload() { //CODEGEN:该调用是 ASP.NET Web 服务设计器所必需的 InitializeComponent(); }
#region Component Designer generated code
//Web 服务设计器所必需的 private IContainer components = null;
/// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { }
/// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if(disposing && components != null) { components.Dispose(); } base.Dispose(disposing); }
#endregion
[WebMethod(Description="Web 服务提供的方法,返回是否文件上载成功与否。")] public string UploadFile(byte[] fs,string FileName) { try { ///定义并实例化一个内存流,以存放提交上来的字节数组。 MemoryStream m = new MemoryStream(fs); ///定义实际文件对象,保存上载的文件。 FileStream f = new FileStream(Server.MapPath(".") + "\\" + FileName, FileMode.Create); ///把内内存里的数据写入物理文件 m.WriteTo(f); m.Close(); f.Close(); f = null; m = null; return "文件已经上传成功。"; } catch(Exception ex) { return ex.Message; } } } } |
上一页 [1] [2] [3] [4] [5] 下一页

【责编:Peng】