webservice

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.IO;
using System.Configuration;

namespace UpDownFile
{
    /// <summary>
    /// UpDownService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class UpDownService : System.Web.Services.WebService
    {
        string keyvalue = ConfigurationManager.AppSettings["SecurityKey"];
        public SecurityHeader securityKey = new SecurityHeader();

        //将Stream流转换为byte数组的方法。
        //PS:原本想把这个方法也当做WebMethod的,因为客户端在上传文件时也要调用该方法,后来发现Stream类型的不能通过WebService传输。。。:(
        public byte[] ConvertStreamToByteBuffer(Stream s)
        {
            MemoryStream ms = new MemoryStream();
            int b;
            while ((b = s.ReadByte()) != -1)
            {
                ms.WriteByte((byte)b);
            }
            return ms.ToArray();
        }

        //上传文件至WebService所在服务器的方法,这里为了操作方法,文件都保存在UpDownFile服务所在文件夹下的File目录中

        [WebMethod]
        [SoapHeader("securityKey")]
        public bool Up(byte[] data, string filename)
        {
            if (securityKey.SecurityKey.Equals(keyvalue))
            {
                try
                {
                    if (!Directory.Exists(Server.MapPath("Images/") + filename.Substring(0, filename.LastIndexOf('\\'))))
                    {
                        Directory.CreateDirectory(Server.MapPath("Images/") + filename.Substring(0, filename.LastIndexOf('\\')));
                    }
                    FileStream fs = File.Create(Server.MapPath("Images/") + filename);
                    fs.Write(data, 0, data.Length);
                    fs.Close();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        //下载WebService所在服务器上的文件的方法

        [WebMethod]
        [SoapHeader("securityKey")]
        public byte[] Down(string filename)
        {
            if (securityKey.SecurityKey.Equals(keyvalue))
            {
                string filepath = Server.MapPath("Images/") + filename;
                if (File.Exists(filepath))
                {
                    try
                    {
                        FileStream s = File.OpenRead(filepath);
                        return ConvertStreamToByteBuffer(s);
                    }
                    catch
                    {
                        return new byte[0];
                    }
                }
                else
                {
                    return new byte[0];
                }
            }
            else
            {
                return new byte[0];
            }
        }
    }
}
posted @ 2011-07-11 16:56  小羽毛的天空  阅读(237)  评论(0编辑  收藏  举报