好吧,我想我的写写了。我用的方法很简单,首先先把大文件转换成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);
Console.WriteLine("Trans end at " + DateTime.Now.ToString());
}
Console.ReadLine();
}
public static byte[] ConvertToBinary(string Path)
{
FileStream stream = new FileInfo(Path).OpenRead();
byte[] buffer = new byte[stream.Length];
Console.WriteLine("The lenght of the file is"+buffer.Length);
stream.Read(buffer, 0, Convert.ToInt32(stream.Length));
return buffer;
}
public static byte[] ConvertToBinary(int index,boolifEnd)//index是第几批,从0开始这个方法名跟上边那个重名了注意区别,两个方法意义是不一样的,我懒得改了
{
//20971520
//index = buffer.Length / 20971520;
//index += buffer.Length % 20971520 == 0 ? 0 : 1;
byte[] bys;//临时二进制数组,最大20M
if (ifEnd == false)
{
bys = new byte[20971520];
for (int ii = 20971520 * index; ii < 20971520 *(index + 1); ii++)
{
bys[ii - 20971520 * index] = buffer[ii];
}
}
else
{
bys = new byte[buffer.Length - 20971520 * (index )];
for (int ii = 20971520 * index; ii < buffer.Length;ii++)
{
bys[ii - 20971520 * index] = buffer[ii];
}
}
Console.WriteLine("The length of the current buffer is " +bys.Length);
return bys;
}
public static byte[] ChangeFileToByte(string path)
{
FileStream stream = new FileInfo(path).OpenRead();
byte[] Filebuffer = new byte[stream.Length];
stream.Read(Filebuffer, 0, Convert.ToInt32(stream.Length));
return Filebuffer;
}
}
}
以下是webservice部分的方法
[WebMethod(Description = "TransFile")]
publicstring TransFile(byte[] fileBt, stringfileName)
{
string rst = "successful";
if (fileBt.Length == 0)
return "The length of the File"+fileName +"is 0";
string filePath =System.Configuration.ConfigurationManager.AppSettings["CSVPath"].ToString();//文件路径
//FileStream fstream = File.Create(filePath + fileName,fileBt.Length);
//FileStream fstream = File.AppendAllText(
FileStream fstream = new FileStream(filePath + fileName,FileMode.Append);
try
{
//MemoryStream m = new MemoryStream(fileBt);
//m.WriteTo(fstream);
fstream.Write(fileBt, 0,fileBt.Length);//二进制转换成文件
fstream.Close();
//rst += "rn";
rst += "File Name is:" + fstream.Name + "rn";
rst += "File Lenght is:" + fstream.Length + "rn";
rst += "File Position is:" + fstream.Position +"rn";
}
catch (Exception ex)
{
//抛出异常信息
rst = ex.ToString();
}
finally
{
fstream.Close();
}
StringBuilder sbd = new StringBuilder();
sbd.AppendLine(rst);
return sbd.ToString();
}