• 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];
                }
            }
        }
    }
    
  • 相关阅读:
    原来实现钉钉自动签到如此简单,每天准时上下班不是梦
    12306 抢票系列之只要搞定RAIL_DEVICEID的来源,从此抢票不再掉线(下)
    12306 抢票系列之只要搞定RAIL_DEVICEID的来源,从此抢票不再掉线(中)
    12306 抢票系列之只要搞定RAIL_DEVICEID的来源,从此抢票不再掉线(上)
    python 学习笔记之手把手讲解如何使用原生的 urllib 发送网络请求
    发生线上故障后问责是不是第一要务
    软件工程是否可以直接应用于小团队
    阶段性正确的一点记录
    Java7新特性
    从server.xml看Tomcat容器的层次结构
  • 原文地址:https://www.cnblogs.com/feathers/p/2103221.html
Copyright © 2020-2023  润新知