客户端代码如下:
Service525.Service1 ser = new WindowsFormsApplication4.Service525.Service1(); private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
string localFileName = ofd.FileName.ToString();
string ziliaoname = Path.GetFileName(localFileName);
int length;
int count1 = 1024 * 100;
FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read);
byte[] bt = new byte[count1];
length = fs.Read(bt, 0, bt.Length);
int i = UploadStream(localFileName, ziliaoname, length);
if (i == 0)
{
MessageBox.Show("成功");
this.Close();
}
}
private int UploadStream(string localFileName, string ziliaoname, int iExists)
{ Stream stream = null;
try {stream = new FileInfo(localFileName).OpenRead();
int size = 1024 * 64;
byte[] buffer = new byte[size];//以每次64K的大小传递流
int index = 0, count = size;
if (stream.Length < size)
{count = (int)stream.Length;
buffer = new byte[stream.Length];}
stream.Position = iExists;
while ((count = stream.Read(buffer, 0, count)) != 0)
{
ser.Upload(ziliaoname, iExists + index, buffer);
index += count;
if (count != size)
{ break; }
}}
catch (Exception ex)
{ MessageBox.Show(ex.Message);}
finally
{ if (stream != null)
{ stream.Close();} }
return 0;
}
}
服务器端代码如下:
[WebMethod]
public bool Upload(string fileName, long offSet, byte[] intoBuffer)
{
//指定上传文件夹+文件名(相对路径)
// string strPath = System.Configuration.ConfigurationManager.AppSettings["filePah"].ToString();
//将相对路径转换成服务器的绝对路径
string strPath1 = Server.MapPath("~/filePah");
if (offSet < 0)
{
offSet = 0;
}
byte[] buffer = intoBuffer;
if (buffer != null)
{
//读写文件的文件流,支持同步读写也支持异步读写
FileStream filesStream = new FileStream(strPath1 + "/" + fileName, FileMode.Create, FileAccess.Write);
filesStream.Seek(offSet, SeekOrigin.Begin);
filesStream.Write(buffer, 0, buffer.Length);
filesStream.Flush();
filesStream.Close();
filesStream.Dispose();
return true;
}
return false;
}
Service525.Service1 ser = new WindowsFormsApplication4.Service525.Service1(); private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
string localFileName = ofd.FileName.ToString();
string ziliaoname = Path.GetFileName(localFileName);
int length;
int count1 = 1024 * 100;
FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read);
byte[] bt = new byte[count1];
length = fs.Read(bt, 0, bt.Length);
int i = UploadStream(localFileName, ziliaoname, length);
if (i == 0)
{
MessageBox.Show("成功");
this.Close();
}
}
private int UploadStream(string localFileName, string ziliaoname, int iExists)
{ Stream stream = null;
try {stream = new FileInfo(localFileName).OpenRead();
int size = 1024 * 64;
byte[] buffer = new byte[size];//以每次64K的大小传递流
int index = 0, count = size;
if (stream.Length < size)
{count = (int)stream.Length;
buffer = new byte[stream.Length];}
stream.Position = iExists;
while ((count = stream.Read(buffer, 0, count)) != 0)
{
ser.Upload(ziliaoname, iExists + index, buffer);
index += count;
if (count != size)
{ break; }
}}
catch (Exception ex)
{ MessageBox.Show(ex.Message);}
finally
{ if (stream != null)
{ stream.Close();} }
return 0;
}
}
服务器端代码如下:
[WebMethod]
public bool Upload(string fileName, long offSet, byte[] intoBuffer)
{
//指定上传文件夹+文件名(相对路径)
// string strPath = System.Configuration.ConfigurationManager.AppSettings["filePah"].ToString();
//将相对路径转换成服务器的绝对路径
string strPath1 = Server.MapPath("~/filePah");
if (offSet < 0)
{
offSet = 0;
}
byte[] buffer = intoBuffer;
if (buffer != null)
{
//读写文件的文件流,支持同步读写也支持异步读写
FileStream filesStream = new FileStream(strPath1 + "/" + fileName, FileMode.Create, FileAccess.Write);
filesStream.Seek(offSet, SeekOrigin.Begin);
filesStream.Write(buffer, 0, buffer.Length);
filesStream.Flush();
filesStream.Close();
filesStream.Dispose();
return true;
}
return false;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货