Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Downloading File via Web Service

Posted on 2007-03-22
3
Medium Priority
?
227 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 1000 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

Technology Partners: 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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

721 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