复杂数据类型在Web Service 上的应用

  我们来考虑一个比较复杂的情况,利用web服务处理比较复杂的数据类型。这里设想需要一个文件服务器,服务器提供一个在服务器上的文件夹所有文件列表,客户可以调用这些文件,当判断为图形文件的时候,直接显示。当判断为其它文件的时候,则保留为当地的文件。同时,还为客户提供了上传数据的能力。
  

using System.IO;

         //调用文件列表

         [WebMethod]

         public string[] callfiles()

         {

              string[] Files;

              Files = Directory.GetFiles("C:""myfile");

        

              //设法把路径去掉

              for (int i=0;i<Files.Length;i++)

              {

                   string S=Files[i];

                   int d = S.IndexOf("""");

                  

                   while (d > 0)

                   {

                       S = S.Substring(d + 1);

                       d = S.IndexOf("""");

                      

                   }

      

                   Files[i]=S;

                  

              }

              return Files;

        

         }

         //下载文件

         [WebMethod]

         public byte[] seefile(string filename)

         {

              byte[] rawData=new byte[0];

              string fl="C:""myfile"""+filename;

             

              try

              {

             

                   //构造一个流对象,文件名是选中的文件

                   FileStream fs = new FileStream(fl, FileMode.OpenOrCreate, FileAccess.Read);

                   //构造一个动态字节数组

                   rawData= new byte[fs.Length];

                   //读流对象,从0开始,长度是fs的长度

                   fs.Read(rawData, 0, System.Convert.ToInt32(fs.Length));

                   fs.Close();

              }

              catch

              {

              }

              return rawData;

         }

         //上传文件

         [WebMethod]

         public bool upload(byte[] filearray,string filename)

         {

              string S=filename;

        

              //如果文件名存在路径,也应该去掉

              int d = S.IndexOf("""");

                  

              while (d > 0)

              {

                   S = S.Substring(d + 1);

                   d = S.IndexOf("""");

                      

              }

              //构造一个本地的路径

              filename="C:""myfile"""+S;

             

              try

              {

                         

                   //构造一个文件流和一个二进制流

                   FileStream fs;

                   BinaryWriter bw;

                      

                   //把数组写进文件

                   fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write);

                   bw = new BinaryWriter(fs);

                   bw.Write(filearray);

                   bw.Close();

                   fs.Close();

                   return true;

              }

              catch

              {

                   return false;

              }       

        

         }

    

测试一下。

posted @ 2008-03-25 13:25  阿豹  阅读(351)  评论(0编辑  收藏  举报
www.bjxzg.cn