Solved

Downloading File via Web Service

Posted on 2007-03-22
3
223 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

734 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