【文件属性】:
文件名称:c# 流断点上传
文件大小:206KB
文件格式:RAR
更新时间:2016-03-18 03:31:58
大文件上传 断点上传
大文件上传 断点上传
客户端
///
/// 服务器端地址,部署时为Web端的地址,可以用配置文件实现
///
public static string serverPath;
///
/// 上传文件(按照文件名称上传,支持断点)
///
/// 待上传文件全路径
/// 上传时的流量控制,文件较大时,用于切割文件上传
/// 错误信息
/// 成功或者失败
public static bool UpLoadFile(string fileName, int byteCount,out string msg)
{
msg = "";
bool result = true;
long cruuent = GetServerFileLength(fileName);
FileStream fStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader bReader = new BinaryReader(fStream);
long length = fStream.Length;
fileName = fileName.Substring(fileName.LastIndexOf('\\') + 1);
#region 开始上传文件
try
{
#region 续传处理
byte[] data;
if (cruuent > 0)
{
fStream.Seek(cruuent, SeekOrigin.Current);
}
#endregion
#region 分割文件上传
for (; cruuent <= length; cruuent = cruuent + byteCount)
{
if (cruuent + byteCount > length)
{
data = new byte[Convert.ToInt64((length - cruuent))];
bReader.Read(data, 0, Convert.ToInt32((length - cruuent)));
}
else
{
data = new byte[byteCount];
bReader.Read(data, 0, byteCount);
}
try
{
Hashtable parms = new Hashtable();
parms.Add("fileName", fileName);
parms.Add("npos", cruuent.ToString());
parms.Add("method", "SaveUpLoadFile");
byte[] byRemoteInfo = PostData(serverPath, data, parms);
}
catch (Exception ex)
{
msg = ex.ToString();
result = false;
break;
}
#endregion
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
bReader.Close();
fStream.Close();
}
GC.Collect();
#endregion
return result;
}
///
/// 调用服务器端方法,或者服务器端,同名称的文件,已经上传的大小
///
/// 待上传文件全路径
/// 服务器端文件的大小,如果以前没有上传过,则返回0
private static long GetServerFileLength(string fileName)
{
fileName = fileName.Substring(fileName.LastIndexOf('\\') + 1);
Hashtable parms = new Hashtable();
parms.Add("fileName", fileName);
parms.Add("method", "GetServerFileLength");
long length = 0;
byte[] data = new byte[0];
byte[] byRemoteInfo = PostData(serverPath, data, parms);
string sRemoteInfo = System.Text.Encoding.Default.GetString(byRemoteInfo);
length = Convert.ToInt64(sRemoteInfo);
return length;
}
///
/// 向服务器端提交数据
///
/// 服务器端地址
/// 要发送的数据
/// 附加参数,用URL的方式将此参数附件到地址中
/// 服务器端返回的 byte[]
private static byte[] PostData(string serverURL, byte[] data, Hashtable parms)
{
System.Net.WebClient webClientObj = new System.Net.WebClient();
if (parms != null)
{
serverURL = serverURL + "?";
foreach (string key in parms.Keys)
{
serverURL = serverURL + key + "=" + parms[key].ToString() + "&";
}
serverURL.TrimEnd('&');
}
byte[] byRemoteInfo = webClientObj.UploadData(serverURL, "POST", data);
return byRemoteInfo;
}
接收端
///
/// 保存文件(从URL参数中获取文件名、当前指针,将文件流保存到当前指针后)
/// 如果是第一次上传,则当前指针为0,代码执行与续传一样,只不过指针没有偏移
///
/// 文件名
/// 当前指针偏移
///
public int SaveUpLoadFile(string fileName, long npos)
{
int upLoadLength = Convert.ToInt32(HttpContext.Current.Request.InputStream.Length);
string file = HttpContext.Current.Server.MapPath("/" + fileName);
//string path = HttpContext.Current.Server.MapPath("/");
//fileName = path + "UpLoad\\" + fileName;
FileStream fStream = new FileStream(file, FileMode.OpenOrCreate, FileAccess.ReadWrite);
//偏移指针
fStream.Seek(npos, SeekOrigin.Begin);
//从客户端的请求中获取文件流
BinaryReader bReader = new BinaryReader(HttpContext.Current.Request.InputStream);
try
{
byte[] data = new byte[upLoadLength];
bReader.Read(data, 0, upLoadLength);
fStream.Write(data, 0, upLoadLength);
return 1;
}
catch
{
//TODO 添加异常处理
}
finally
{
//释放流
fStream.Close();
bReader.Close();
}
return -1;
}
【文件预览】:
DuanDanUploadTest
----client()
--------client.csproj(4KB)
--------bin()
--------Form1.Designer.cs(3KB)
--------Program.cs(487B)
--------obj()
--------Form1.cs(1KB)
--------Form1.resx(6KB)
--------UpLoadLib.cs(8KB)
--------Properties()
----DuanDanUploadTest.sln(3KB)
----DuanDanUploadTest()
--------bin()
--------Site.Master.cs(349B)
--------Default.aspx(171B)
--------Account()
--------Site.Master.designer.cs(3KB)
--------Web.config(2KB)
--------Styles()
--------obj()
--------Web.Debug.config(1KB)
--------DuanDanUploadTest.csproj.user(1KB)
--------Default.aspx.designer.cs(474B)
--------httpposted.aspx(125B)
--------httpposted.aspx.designer.cs(476B)
--------App_Data()
--------Web.Release.config(1KB)
--------DuanDanUploadTest.csproj(7KB)
--------httpposted.aspx.cs(2KB)
--------Global.asax.cs(1KB)
--------Site.Master(2KB)
--------Global.asax(101B)
--------Scripts()
--------Properties()
--------Default.aspx.cs(3KB)
----DuanDanUploadTest.suo(35KB)