Click here to Skip to main content
6,629,377 members and growing! (20,700 online)
Email Password   helpLost your password?
Web Development » Web Services » General     Intermediate License: The Microsoft Public License (Ms-PL)

Transfer any files on Web services by c#

By John.Jiang

We can use Microsoft SOAP protocol to transfer any files between client and server.It's easy through firewall and also it cross flatform.
C#, .NET (.NET 2.0), Win32, Dev
Posted:11 Sep 2008
Views:14,102
Bookmarked:27 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
12 votes for this article.
Popularity: 2.04 Rating: 1.89 out of 5
7 votes, 58.3%
1
1 vote, 8.3%
2
2 votes, 16.7%
3

4
2 votes, 16.7%
5

Introduction

We can use Microsoft SOAP protocol to transfer any files between client and server.It's easy through firewall and also it cross flatform.

Background

I was going to develop an mobile software on pocket pc,but I found there were tiny resouces to use on mobile operating system.finally,I was finding Web services can solve the problem.

Using the code

There are two parts on the sample which included: Windows client and Webservice server code. 



using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace www.treaple.com
{
    class TransferFile
   {
       public TransferFile() { }

        public string WriteBinarFile(byte[] fs, string path, string fileName)
        {
            try
            {
                MemoryStream memoryStream = new MemoryStream(fs);
                FileStream fileStream = new FileStream(path + 
                fileName,  FileMode.Create);
                memoryStream.WriteTo(fileStream);
                memoryStream.Close();
                fileStream.Close();
                fileStream = null;
                memoryStream = null;
                return "File has already uploaded successfully。";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        /// getBinaryFile:Return array of byte which you specified。
         
        public byte[] ReadBinaryFile(string path,string fileName)
        {
            if (File.Exists(path + fileName))
            {
                try
                {
                  ///Open and read a file。
                       FileStream fileStream = File.OpenRead(path + fileName);
                    return ConvertStreamToByteBuffer(fileStream);
                }
                catch (Exception ex)
                {
                    return new byte[0];
                }
            }
            else
            {
                return new byte[0];
            }
        }

        /// ConvertStreamToByteBuffer:Convert Stream To ByteBuffer。

        public 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));
            }
            return tempStream.ToArray();
        }
    }
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace www.treaple.com
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //define and Initialize object 
        private GetImage.Service getImage = new GetImage.Service();
        private TransferFile transferFile = new TransferFile();

        private void BtnOpen_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = "C:/";
            openFileDialog1.Filter = "All 
            Files|*.*"; //"All Files|*.*|Bitmaps|*.bmp|GIFs|*.gif|JPEGs|*.jpg";
            openFileDialog1.FilterIndex = 2;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
           {
                txtUpPath.Text = openFileDialog1.FileName;
            }
        }
        private void BtnUploadFile_Click(object sender, EventArgs e)
        {
            MessageBox.Show(getImage.UploadFile(
                            transferFile.ReadBinaryFile(this.txtUpPath.Text, 
                            this.txtUpName.Text), this.txtUpName.Text));  
        }

        private void BtnDownloadFile_Click(object sender, EventArgs e)
        {
            MessageBox.Show(transferFile.WriteBinarFile(
                            getImage.DownloadFile(txtDownName.Text.Trim()), 
                            this.txtLocalPath.Text, this.txtLocalName.Text));
        }

        private void linkLabel1_LinkClicked(
         object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.treaple.com");
        }

        private void linkLabel2_LinkClicked(
              object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.treaple.com");
        }
    }
}



//Server:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace www.treaple.com

    class TransferFile
    {
        TransferFile() { }

        private string WriteBinarFile(byte[] fs, string path, 
        string fileName)
        {
            try
            {
                MemoryStream memoryStream = new MemoryStream(fs);
                FileStream fileStream = new FileStream(
                path + fileName, FileMode.Create);
                memoryStream.WriteTo(fileStream);
                memoryStream.Close();
                fileStream.Close();
                fileStream = null;
                memoryStream = null;
                return ("File has already uploaded successfully");
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        /// getBinaryFile return array of byte which you specified

        public byte[] ReadBinaryFile(string path,string fileName)
        {
            if (File.Exists(path + fileName))
            {
                try
                {
                    ///Open and read a file
                    FileStream fileStream = File.OpenRead(path + fileName);
                    return ConvertStreamToByteBuffer(fileStream);
                }
                catch (Exception ex)
                {
                    return new byte[0];
                }
            }
            else
            {
                return new byte[0];
            }
        }

        /// 
        /// ConvertStreamToByteBuffer convert Stream To ByteBuffer
        /// 
        /// 
        /// 
        public 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));/
            }
            return tempStream.ToArray();
        }
    }
}

		

History

If you have any questions about on the source code.Please visit www.treaple.com to contact me.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

About the Author

John.Jiang


Member
Treaple offshore outsourcing software services providing mobile(Pocket pc,smartphone and wince.net) software,mobile GIS(Mobile map),Desktop GIS(Desktop map), GPS,GSM Locating Services,Voip,Multimedia(Audio,Video) and web design offshore outsourcing software development services.We have developed lots of projects on Microsoft Poccket pc 5.0/6.0,smartphone 5.0/6.0 and Microsoft windows and got strong background in Microsoft MapPoint, ESRI ArcGIS, Map info,Google map etc
Our website below: http://www.treaple.com
Occupation: Software Developer (Senior)
Company: www.treaple.com
Location: China China

Other popular Web Services articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 11 of 11 (Total in Forum: 11) (Refresh)FirstPrevNext
GeneralMy vote of 1 Pinmembertomtom198022:49 29 Jul '09  
GeneralThanks! PinmemberDanie de Kock1:01 10 Jul '09  
GeneralRe: Thanks! PinmemberJohan "BJ" Vorster4:35 21 Jul '09  
GeneralRe: Thanks! PinmemberDanie de Kock10:23 21 Jul '09  
GeneralRe: Thanks! PinmemberDanie de Kock10:23 21 Jul '09  
GeneralRe: Thanks! PinmemberJohan "BJ" Vorster23:17 21 Jul '09  
GeneralRe: Thanks! PinmemberDanie de Kock4:27 22 Jul '09  
QuestionServer app Pinmemberhahacasd17:42 13 Jan '09  
GeneralMy vote of 1 Pinmembervalmir19694:28 13 Jan '09  
GeneralNo Explinations PinmemberHarshdeep Mehta (4700787)5:15 12 Sep '08  
GeneralRubbish Article PinmemberThe Ruler23:46 11 Sep '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 11 Sep 2008
Editor: Sean Ewington
Copyright 2008 by John.Jiang
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project