在本Blog的WebService有一個關于上傳文件的文章!
而這個就是所說的winform代碼!!
長話短說......直接貼代碼了!有疑問請看http://yaka.cnblogs.com/archive/2006/06/04/416910.html
winform同樣的也要引用Microsoft.Web.Services2.dll~在安裝目錄能夠找到!C:\Program Files\Microsoft WSE\v2.0
當然更加要引用之前做好的Web Service~
導入兩個命名空間~
using Microsoft.Web.Services2;
using Microsoft.Web.Services2.Dime;
private
void
Send()
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
byte [] b = null;
//Web Service class
ET.Service1 et = new ET.Service1();
//this.finfo.FullName 要上傳的文件名
string name = this.finfo.Name;
FileStream f = new FileStream(name,FileMode.Open,FileAccess.Read,FileShare.Read);
//文件总长度
long totalLength = f.Length;
//每次讀取數據的長度
int length = 128 * 1024;
//偏移量
long offset = 0;
//直到offset超齣範圍
while(totalLength > offset)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//原定讀取長度大于可讀取長度
if( partLength - offset < length )
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
length = (int)(partLength - offset);
}
//移動起始位置
f.Seek(offset,SeekOrigin.Begin);
//讀取文件
b = new byte[length];
f.Read(b,0,b.Length);
//清空緩衝區
f.Flush();
//將offset與發送出去的長度纍加,
try
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//將要上傳的文件流作為附件
MemoryStream myMemoryStream = new MemoryStream(b, 0,length);
DimeAttachment dimeAttach = new DimeAttachment("audio/unknown", TypeFormat.Unknown, myMemoryStream);
//嚮相應的WebService添加附件
et.RequestSoapContext.Attachments.Add(dimeAttach);
//開始上傳,接收成功上傳的數據長度
int num = et.Upload(name,offset);
//將offset與發送出去的長度纍加
offset = offset + num;
}
catch(Exception e)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
f.Close();
}
這段代碼就是上傳的核心~~當然我的程序是多段,多綫程,可以續傳的!所以上麵的代碼有一少部分作了修改,那些功能就讓大家去寫吧!~呵呵~
但是還有一個註意的地方!改一個東西~要不就會編譯齣錯!在Reference.cs裏麵的service類繼承為Microsoft.Web.Services2.WebServicesClientProtocol
转载于:https://www.cnblogs.com/YaKa/archive/2006/06/04/416816.html