Solved

Downloading File via Web Service

Posted on 2007-03-22
3
222 Views
Last Modified: 2010-04-16
Hi Everyone,
  Is it possible to download a binary or zip file from a server via web service?If yes is there anyone here who could provide me the sample code or idea, Thanks a lot!!
0
Comment
Question by:xms_dev
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
jef06 earned 250 total points
ID: 18778019
Yes you can i pass the file as a string but you need to convert your binary in base64string
i use GetFile to read and convert in string on the web service

#region GetFile
            /// <summary>
            /// retourne les données d'un fichier dans une string base64
            /// </summary>
            /// <param name="filename"></param>
            /// <param name="version"></param>
            /// <param name="ext"></param>
            /// <returns></returns>
            public static string GetFile(string filename)
            {
                  byte[] binaryData;
                  binaryData = getBinaryFile(filename));
                  return System.Convert.ToBase64String(binaryData, 0, binaryData.Length);
            }
            #endregion

            #region getBinaryFile
            /// <summary>
            /// récupère les donnée du ficher et les mets dans tableau de byte
            /// </summary>
            /// <param name="filename"></param>
            /// <returns></returns>
            private static byte[] getBinaryFile(string filename)
            {
                  if(File.Exists(filename))
                  {
                        FileStream fs = null;
                        try
                        {
                              fs=File.OpenRead(filename);
                              return ConvertStreamToByteBuffer(fs);
                        }
                        catch(Exception e)
                        {
                              string text = e.Message;
                              return new byte[0];
                        }
                        finally
                        {
                              fs.Close();
                        }
                  }
                  else
                  {
                        return new byte[0];
                  }
            }
            #endregion

            #region ConvertStreamToByteBuffer
            /// <summary>
            /// Convertit un stream en un tableau de byte
            /// </summary>
            /// <param name="theStream"></param>
            /// <returns></returns>
            private static byte[] ConvertStreamToByteBuffer(System.IO.Stream theStream)
            {
                  int b1;
                  System.IO.MemoryStream tempStream = new System.IO.MemoryStream();
                  while((b1=theStream.ReadByte())!=-1)
                  {
                        tempStream.WriteByte(((byte)b1));
                  }
                  byte[] tmp = tempStream.ToArray();
                  tempStream.Close();
                  return tmp;
            }
            #endregion

to read on the client you use
string filedata = GetFile(...)
byte[] filexmlBytes;
filexmlBytes = System.Convert.FromBase64String(filedata);

You can also use SharZipLib to compress data before sending them
0
 

Author Comment

by:xms_dev
ID: 18778306
Hi Jef06
Your sample code works perfectly for our needs Thank you very much!!!
0
 
LVL 5

Expert Comment

by:jef06
ID: 18778319
Great can you affect point :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question