Wednesday, January 8, 2014

Transfer and Receive your Images From App to Webservice


Transfer and Receive your Images(Files) From App to Web service 

How would you send image file(or any other file) from Application to Web service..??

The Answer is you can do it with converting the Image int to Byte[] Array then  ToBase64String  & at destination do the Reverse Process to collect the image.

(Or)

Directly you can pass byte array & Convert at destination


I have the requirement to upload the images to the sync service,that's I am sharing with you...


At Source(Send Image/File)

  /// <summary>
        /// Converting file to bytes
        /// </summary>
        /// <param name="filename">Path of the file</param>
        /// <returns>File data in form of bytes</returns>
        private byte[] GetFileByteArray(string filename)
        {
            FileStream oFileStream = new FileStream(filename, FileMode.Open,     FileAccess.Read);
            // Create a byte array of file size.
            byte[] FileByteArrayData = new byte[oFileStream.Length];
            //Read file in bytes from stream into the byte array
            oFileStream.Read(FileByteArrayData, 0, System.Convert.ToInt32(oFileStream.Length));
            //Close the File Stream
            oFileStream.Close();
            return FileByteArrayData; //return the byte data
        }


Passing:

Global.SyncClient.UploadFiles(Convert.ToBase64String(GetFileByteArray(file)), Path.GetFileName(file));

At Destination(Receive Image):  
 
/// <summary>
        /// Converting bytes to file
        /// </summary>
        /// <param name="FileData">Received File string format</param>
        /// <param name="FileName">Name of the file</param>
        /// <returns>File data in form of bytes</returns>
/// </summary>

  public void UploadFiles(string FileData, string FileName)
        {
            try
            {
                string filePath = ConfigurationManager.AppSettings[""].ToString();
                byte[] fileData = Convert.FromBase64String(FileData);
                string fileName = filePath + "\\" + FileName;
                Utilities.LogStatus(fileName);
                FileStream fs = new FileStream(fileName,  FileMode.Create,FileAccess.Write);
                fs.Write(fileData, 0, fileData.Length);    
                fs.Close();
            }
            catch (Exception ex)
            {
                Utilities.LogException(ex, "UploadFiles");
            }



Direct Way ==> Image to Bytes[] & Vice Versa:

First method: Convert Image to byte[] array(At Source):
 public byte[] imageToByteArray(System.Drawing.Image imageIn)

{

 MemoryStream img = new MemoryStream(); 

 imageIn.Save(img,System.Drawing.Imaging.ImageFormat.Gif); 

 return  img.ToArray();

}

This method uses the System.Drawing.Image.Save method to save the image to a memorystream. The memorystream can then be used to return a byte array using the ToArray() method in the MemoryStream class.

Second method: Convert byte[] array to Image(At Destination):
public Image byteArrayToImage(byte[] byteArrayIn)

{

     MemoryStream ms = new MemoryStream(byteArrayIn);

     return Image.FromStream(ms);

}

This method uses the Image.FromStream method in the Image class to create a method from a memorystream which has been created using a byte array. The image thus created is returned in this method.
 Hope it helps you.....
                                   
 




No comments: