Friday, August 22, 2014

web service get file and put file

I converted the return of get file to a string.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace TestWebService
{
   /// <summary>
   /// Summary description for WebService1
   /// </summary>
   [WebService(Namespace = "http://tempuri.org/")]
   [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
   [System.ComponentModel.ToolboxItem(false)]
   // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
   // [System.Web.Script.Services.ScriptService]
   public class WebService1 : System.Web.Services.WebService
   {

      [WebMethod]
      public string HelloWorld()
      {
         return "Hello World";
      }

      [WebMethod]
      public string GetFile(string filename)
     // public byte[] GetFile(string filename)
      {
         BinaryReader binReader = new
  BinaryReader(File.Open(Server.MapPath(filename), FileMode.Open,
  FileAccess.Read));
         binReader.BaseStream.Position = 0;
         byte[] binFile =
  binReader.ReadBytes(Convert.ToInt32(binReader.BaseStream.Length));
         binReader.Close();
         var str = System.Text.Encoding.Default.GetString(binFile);
         return str;
      }

      [WebMethod]
      public void PutFile(byte[] buffer, string filename)
      {
         BinaryWriter binWriter = new
  BinaryWriter(File.Open(Server.MapPath(filename), FileMode.CreateNew,
  FileAccess.ReadWrite));
         binWriter.Write(buffer);
         binWriter.Close();


      }
   }
}

below is code to run the web methods

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestAccessWebService
{
   class Program
   {
      static void Main(string[] args)
      {


       //  ServiceReference1.RolesServicePortClient cli = new ServiceReference1.RolesServicePortClient();
        // ServiceReference1.Role[] r ;
       
         //r = cli.getGlobalRolesByEmployee("0001445");
        // ServiceReference1.Role r1;
         //r1 = r[0];

         net.azurewebsites.smallgroup.WebService1 obj = new net.azurewebsites.smallgroup.WebService1();

         //myservicereference.WebService1 obj = new myservicereference.WebService1();
         //Console.WriteLine(r1.ToString());
         //Console.WriteLine(r1.OrgEntityID);
         Console.WriteLine("---------------------");
         //Console.WriteLine("\n Calling show Method");
         Console.WriteLine("  " + obj.HelloWorld());
         Console.WriteLine("\n\n" + " Calling GetFile Method ");
         byte[] bytearray = obj.GetFile("storelist.txt");
         //byte[] bytearray = obj.GetFile("StopSQL.bat");
         //Console.WriteLine("---------------------");
         var str = System.Text.Encoding.Default.GetString(bytearray);
         Console.WriteLine(str.ToString());
         Console.WriteLine("---------------------");
         //obj.PutFile(bytearray, "storelist.txt");
         //bytearray = obj.GetFile("storelist.txt");
         Console.WriteLine("---------------------");
         //str = System.Text.Encoding.Default.GetString(bytearray);
         //Console.WriteLine(str.ToString());
      }
   }
}

No comments:

Post a Comment