web 2.0

Envoi de gros fichiers par Web Service

Bonjour !

L'astuce d'aujourd'hui est de pouvoir envoyer des fichiers de taille illimités par Web Service. Pour ce faire nous allons envoyer le fichier en plusieurs parties.

Code de l'application Cliente (celle qui va envoyer le fichier)

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); 
                } 
            } 
        } 
    } 
} 


Code de l'application Serveur (Le Web Service permettant de recevoir le fichier)

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); 
            } 
        } 
    } 
} 

Téléchargez ici les sources de cet article SendLargeFileWebService.rar (28,88 kb)

Yoann,



Views(489)

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