Solved

Downloading File via Web Service

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c# LinkButton OnClientClick 2 34
Need to pass connection string through config 2 26
C# parent child form 5 19
User Authentication using Digital Certificate 2 26
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 describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 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

15 Experts available now in Live!

Get 1:1 Help Now