web 2.0

Sending Big Files Through WebService using Chunk with C#

Hi,

Today I'll talking about Sending Big Files through WebService using Chunk with C#. This article is a translation of my previous french article.

Here is the Client Side Code

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Client.FileService; 
using System.IO; 

namespace Client 
{ 
    class Program 
    { 
        static void Main(string[] args) 
        { 
            using (FileServiceSoapClient client = new FileServiceSoapClient()) 
            { 
                Console.WriteLine("Enter File Path"); 
                String FilePath = Console.ReadLine(); 
                long fileSize = new FileInfo(FilePath).Length; 
                int chunkSize = 1000 * 1024; 
                byte[] buffer = new byte[chunkSize]; 
                long offSet = 0; 
                String FileName = Path.GetFileName(FilePath); 

                using (FileStream fs = File.Open(FilePath, FileMode.Open, FileAccess.Read)) 
                { 
                    int byteRead = 0; 
                    fs.Position = offSet; 

                    do 
                    { 
                        byteRead = fs.Read(buffer, 0, chunkSize); 
                        if (byteRead != buffer.Length) 
                        { 
                            chunkSize = byteRead; 
                            byte[] trimmedBuffer = new byte[byteRead]; 
                            Array.Copy(buffer, trimmedBuffer, byteRead); 
                            buffer = trimmedBuffer; 
                        } 
                        if (buffer.Length == 0) 
                            break; 
                        try 
                        { 
                            client.UploadFile(FileName, buffer, offSet); 
                            offSet += byteRead; 
                        } 
                        catch 
                        { 
                            break; 
                        } 
                    } 
                    while (byteRead > 0); 
                } 
            } 
        } 
    } 
} 

Here is the Web Service Server Side Code

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Services; 
using System.IO; 

namespace Server 
{ 
    /// <summary> 
    /// Summary description for FileService 
    /// </summary> 
    [WebService(Namespace = "http://tempuri.org/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    [System.ComponentModel.ToolboxItem(false)] 
    public class FileService : System.Web.Services.WebService 
    { 

        [WebMethod] 
        public void UploadFile(String FileName, byte[] buffer, long Offset) 
        { 
            String FilePath = Path.Combine(@"C:\Temp", FileName); 

            if (!Directory.Exists(Path.GetDirectoryName(FilePath))) 
            { 
                Directory.CreateDirectory(Path.GetDirectoryName(FilePath)); 
            } 
            if (Offset == 0) 
            { 
                File.Create(FilePath).Close(); 
            } 

            using (FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read)) 
            { 
                fs.Seek(Offset, SeekOrigin.Begin); 
                fs.Write(buffer, 0, buffer.Length); 
            } 
        } 
    } 
} 

 

Hope this help's!

Download Source Code



Views(1879)

kick it on DotNetKicks.com

Share/Save/Bookmark Subscribe

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

C# | Web Service

Comments

Add comment


 

biuquote
Loading



Technorati Profile