locked
Retrieving a file from webservice RRS feed

  • Question

  • User-151352204 posted

    I using external web service that has a method to download a file by passing some parameters..

    I use the method and pass required parameters and receiving filename, file type, file content length and other stuff.

    How to download a file from here?

    Thanks

    Wednesday, October 30, 2013 7:49 PM

Answers

  • User-1623675128 posted
    private List<byte[]> ContentToByteArray(DataTable dt)
            {
    
                DataTable docProperties = new DataTable();
                List<byte[]> docContent = new List<byte[]>();
    
                foreach (DataRow dr in dt.Rows)
                {
                    docContent.Add((byte[])dr["docContent"]);
                }
                return docContent;
            }
     
    this will return byte content from here you can create the file using like....
    public bool ByteArrayToFile(string _FileName, byte[] _ByteArray)
    {
       try
       {
          // Open file for reading
          System.IO.FileStream _FileStream = 
             new System.IO.FileStream(_FileName, System.IO.FileMode.Create,
                                      System.IO.FileAccess.Write);
          // Writes a block of bytes to this stream using data from
          // a byte array.
          _FileStream.Write(_ByteArray, 0, _ByteArray.Length);
    
          // close file stream
          _FileStream.Close();
    
          return true;
       }
    
    hope this helps

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 31, 2013 1:58 AM
  • User-488622176 posted

    You are currently doing this:

    • You have a webservice
    • You sent some parameters to it => request message
    • You get some information on the file based upon the parameters => response message

    Now you want to either extend the response message with the file data, or create a new request message to get the file and a new response message with the file data. In the last case you'll need a new method on your web service.

    Some illustrative service code:

    [DataContract]
    public class FileInfo
    {
      [DataMember]
      public int FileSize {get;set;}
      [DataMember]
      public byte[] FileData {get;set;}
    }
    
    [ServiceContract]
    public interface IMyFileService
    {
      [OperationContract]
      FileInfo GetFileInfo(int fileID);
    }
    
    public class MyFileService : IMyFileService
    {
      public FileInfo GetFileInfo(int fileID)
      {
        FileInfo res = new FileInfo();
        // get the filename ... (sample)
        res.FileData = System.IO.File.ReadAllBytes(fnm);
        return res;
      }
    }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 31, 2013 5:38 AM

All replies

  • User-1623675128 posted

    I guess you msut be recievign the content also from the webservice along with above details then you have to write that content into a file you need to make that up on client end.

    we have a service where we are consuming it which sends all the details like you mentioned along with file content (with type also like BASE64/binary etc) then we will make it up as a file using its details on client end.

    Thursday, October 31, 2013 1:30 AM
  • User1811844460 posted
    Then. How to do

    Thursday, October 31, 2013 1:47 AM
  • User-1623675128 posted
    private List<byte[]> ContentToByteArray(DataTable dt)
            {
    
                DataTable docProperties = new DataTable();
                List<byte[]> docContent = new List<byte[]>();
    
                foreach (DataRow dr in dt.Rows)
                {
                    docContent.Add((byte[])dr["docContent"]);
                }
                return docContent;
            }
     
    this will return byte content from here you can create the file using like....
    public bool ByteArrayToFile(string _FileName, byte[] _ByteArray)
    {
       try
       {
          // Open file for reading
          System.IO.FileStream _FileStream = 
             new System.IO.FileStream(_FileName, System.IO.FileMode.Create,
                                      System.IO.FileAccess.Write);
          // Writes a block of bytes to this stream using data from
          // a byte array.
          _FileStream.Write(_ByteArray, 0, _ByteArray.Length);
    
          // close file stream
          _FileStream.Close();
    
          return true;
       }
    
    hope this helps

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 31, 2013 1:58 AM
  • User-488622176 posted

    You are currently doing this:

    • You have a webservice
    • You sent some parameters to it => request message
    • You get some information on the file based upon the parameters => response message

    Now you want to either extend the response message with the file data, or create a new request message to get the file and a new response message with the file data. In the last case you'll need a new method on your web service.

    Some illustrative service code:

    [DataContract]
    public class FileInfo
    {
      [DataMember]
      public int FileSize {get;set;}
      [DataMember]
      public byte[] FileData {get;set;}
    }
    
    [ServiceContract]
    public interface IMyFileService
    {
      [OperationContract]
      FileInfo GetFileInfo(int fileID);
    }
    
    public class MyFileService : IMyFileService
    {
      public FileInfo GetFileInfo(int fileID)
      {
        FileInfo res = new FileInfo();
        // get the filename ... (sample)
        res.FileData = System.IO.File.ReadAllBytes(fnm);
        return res;
      }
    }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 31, 2013 5:38 AM