Solved

Downloading File via Web Service

Posted on 2007-03-22
3
216 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now