1. 程式人生 > >webservice大檔案傳輸

webservice大檔案傳輸

 好吧,我想我的寫寫了。我用的方法很簡單,首先先把大檔案轉換成byte陣列,然後呼叫webservice將byte陣列轉換成檔案。但是如果檔案大小超過25M的時候會超時,所以我將這個陣列分批傳輸,每一批都是一個20M的byte陣列,然後把這些陣列追加,最後形成一個完整的檔案。注意在形成檔案的時候要判斷要生成的檔案是否存在,否則如果檔案已經存在的話,該檔案不會被覆蓋,而是被追加。

using System;
using System.Collections.Generic;

using System.Text;
using System.IO;

namespace controtest
{
    classProgram
    {
       static byte[] buffer;
       static void Main(string[] args)
       {
           string rst =string.Empty ;
           PartnerWebservice.PartnerServices pa = newPartnerWebservice.PartnerServices();
           string path =System.Configuration.ConfigurationSettings.AppSettings["path"].ToString();//檔案的存放目錄
           string filename =System.Configuration.ConfigurationSettings.AppSettings["name"].ToString();//檔名稱
           Console.WriteLine("ConvertToBinary start at"+DateTime.Now.ToString());
           buffer = ConvertToBinary(path);//將檔案轉換成byte陣列
           int   index = buffer.Length /20971520;//20971520bite就是20M,1*1024*1024*20
           index += buffer.Length % 20971520 == 0 ? 0 : 1;
           bool ifEnd=false;//是否為最後一組
           for (int ii = 0; ii < index; ii++)
           {
               if (ii == index - 1)
                   ifEnd = true;
               Console.WriteLine("ConvertToBinary end at " +DateTime.Now.ToString());
               Console.WriteLine("Trans start at " +DateTime.Now.ToString());
               pa.TransFile(ConvertToBinary(ii, ifEnd),"XXXX.XXX");//分批傳輸資料,追加到XXXX.XXX檔案,如果不存在會自動建立
               Console.WriteLine(rst);