发布于 2016-05-26 05:48:49 | 256 次阅读 | 评论: 0 | 来源: 网友投递
ASP.NET
ASP.NET 是.NET FrameWork的一部分,是一项微软公司的技术,是一种使嵌入网页中的脚本可由因特网服务器执行的服务器端脚本技术,它可以在通过HTTP请求文档时再在Web服务器上动态创建它们。 指 Active Server Pages(动态服务器页面) ,运行于 IIS(Internet Information Server 服务,是Windows开发的Web服务器)之中的程序 。
using System;
using System.IO;
using Microsoft.Web.Services2;
using Microsoft.Web.Services2.Dime;
namespace Webb.WAVE.WinUpload
{
/**//// <summary>
/// Summary description for Controls.
/// </summary>
public class UploadInstance2
{
Fields#region Fields
private string m_GUID;
private DateTime m_uploadTime;
private long m_fileLength;
private long m_currentPoint;
private string m_pathOnserver;
private long m_userID;
#endregion
Properties#region Properties
public long UserID
{
get{return this.m_userID;}
set{this.m_userID=value;}
}
public string GUID
{
get{return this.m_GUID;}
set{this.m_GUID=value;}
}
public DateTime UploadTime
{
get{return this.m_uploadTime;}
set{}
}
public long FileLength
{
get{return this.m_fileLength;}
set{this.m_fileLength=value;}
}
public long CurrentPoing
{
get{return this.m_currentPoint;}
set{this.m_currentPoint=value;}
}
public string PathOnServer
{
get{return this.m_pathOnserver;}
set{this.m_pathOnserver=value;}
}
public string FullPathOnServer
{
get
{
if(this.m_GUID!=string.Empty&&this.m_pathOnserver!=string.Empty)
{
return Path.Combine(this.m_pathOnserver,this.m_GUID+".rem");
}
else
{
return string.Empty;
}
}
}
public string FileName
{
get
{
if(this.m_GUID!=string.Empty)
{
return this.m_GUID+".rem";
}
else
{
return string.Empty;
}
}
}
#endregion
public UploadInstance2()
{
this.m_GUID = System.Guid.NewGuid().ToString();
this.m_uploadTime = System.DateTime.Now;
this.m_currentPoint = 0;
this.m_fileLength = 0;
this.m_pathOnserver = string.Empty;
}
public UploadInstance2(string i_path,string i_GUID,long i_fileLength)
{
string m_fullPath = Path.Combine(i_path,i_GUID);
if(!File.Exists(m_fullPath)) return;
this.m_GUID = i_GUID;
this.m_uploadTime = System.DateTime.Now;
this.m_pathOnserver = i_path;
FileInfo m_fileInfo = new FileInfo(m_fullPath);
this.m_currentPoint = m_fileInfo.Length;
this.m_fileLength = i_fileLength;
}
public bool UploadData(byte[] i_data, long i_currentPoint, int i_dataSize)
{
string m_fullPath = this.FullPathOnServer;
if(!File.Exists(m_fullPath)&&this.m_currentPoint!=0)return false;
long m_filePoint = new FileInfo(m_fullPath).Length;
if(m_filePoint!=i_currentPoint) return false;
FileStream m_fileStream = new FileStream(m_fullPath,FileMode.Append);
m_fileStream.Write(i_data,0,i_dataSize);
m_fileStream.Close();
return true;
}
public void AbandantUpload()
{
string m_fullPath = this.FullPathOnServer;
try{File.Delete(m_fullPath);}
catch{}
}
public void CreateFile()
{
string m_fullPath = this.FullPathOnServer;
if(!File.Exists(m_fullPath))
{
File.Create(m_fullPath).Close();
}
else
{
try
{
File.Delete(m_fullPath);
}catch{}
File.Create(m_fullPath).Close();
}
}
}
}
#region UploadProcess
public void UploadProcess()
{
DateTime m_start = DateTime.Now;
this.textBox_OutMsg.AppendText("Initialize upload\r\n");
if(this.m_upload==null||this.m_uploadGUID==null||this.m_uploadGUID==string.Empty)
{
this.textBox_OutMsg.AppendText("Upload instance id error or login to the server faild\r\n");
this.textBox_OutMsg.AppendText("Upload faild.\r\n");
return;
}
this.textBox_OutMsg.AppendText("Open file\r\n");
if(this.m_filePath==null||this.m_filePath==string.Empty||!File.Exists(this.m_filePath))
{
this.textBox_OutMsg.AppendText("Open file error\r\n");
this.textBox_OutMsg.AppendText("Upload faild.\r\n");
return;
}
FileInfo m_fileInfo = new FileInfo(this.m_filePath);
FileStream m_fs = new FileStream(this.m_filePath, FileMode.Open, FileAccess.Read);
this.textBox_OutMsg.AppendText("Start upload file\r\n");
int m_buffer = 10; //KBytes
long m_currentPoint = 0;
long m_fileLength = m_fileInfo.Length;
bool m_uploadResult = false;
byte[] m_data = new byte[m_buffer*1024];
long m_readBytes = m_fs.Read(m_data, 0, m_buffer*1024);
this.UploadProcessBar.Maximum = 100;
this.UploadProcessBar.Minimum = 0;
while(m_readBytes>0)
{
MemoryStream m_memoryStream = new MemoryStream(m_data, 0,(int)m_readBytes);
DimeAttachment dimeAttach = new DimeAttachment("image/gif", TypeFormat.MediaType, m_memoryStream);
this.m_upload.RequestSoapContext.Attachments.Add(dimeAttach);
m_uploadResult = this.m_upload.UploadFileData(this.m_uploadInstance,m_currentPoint,m_readBytes);
if(m_uploadResult)
{
m_currentPoint +=m_readBytes;
m_readBytes = m_fs.Read(m_data,0,m_buffer*1024);
// this.textBox_OutMsg.AppendText("Uploading:"+m_currentPoint.ToString()+"/"+m_fileLength.ToString()+"\r\n");
this.UploadProcessBar.Value = (int)(m_currentPoint*100/m_fileLength);
this.label_outPercent.Text = this.UploadProcessBar.Value.ToString()+"%";
}
else
{
this.textBox_OutMsg.AppendText("Upload file error.\r\n");
m_fs.Close();
this.m_upload.AbandantUpload(this.m_uploadInstance);
return;
}
}
this.textBox_OutMsg.AppendText("File upload finished.\r\n");
this.button_Cancel.Enabled = false;
m_fs.Close();
this.ResetForm();
}
#endregion