silverlight用webclient大文件上传的实例代码
网络编程 2021-07-04 22:40www.168986.cn编程入门
这篇文章介绍了silverlight用webclient大文件上传的实例代码,有需要的朋友可以参考一下
客户端
/// <summary>
/// 写入数据到流中
/// </summary>
/// <param name="url"></param>
/// <param name="callback"></param>
public async static Task<bool> Write(string url, Stream clientStream)
{
if (clientStream.Length > 2510241024)
url += "&t=1"; // 表示上传大文件
try
{
Up(url, clientStream);
return true;
}
catch { }
return false;
}
public async static Task Up(string url, Stream sourceStream)
{
var wc = new WebClient();
byte[] buffer = new byte[2510241024];
int bufLen = sourceStream.Read(buffer, 0, buffer.Length);
if (bufLen < 1)
{
sourceStream.Close();
return;
}
wc.WriteStreamClosed += (s, e) =>
{
if (sourceStream.CanRead)
Up(url, sourceStream);
else
sourceStream.Close();
};
var serverStream = await wc.OpenWriteTaskAsync(url, "POST");
serverStream.Write(buffer, 0, bufLen);
serverStream.Close();
}
服务端
private void Save()
{
string data = Context.Request.QueryString["data"].Base64StringDecode("ABC");
if (data.IsNullOrEmpty())
return;
var m = JsonConvert.DeserializeObject<FileUploadModel>(data);
if (m == null)
return;
var isSplitBlock = Context.Request.QueryString["t"]=="1"; //是否分块上传
#region 保存文件
// 初始化目录
string dirPath = Path.Combine(ConfigHelper.UploadPath, m.Dir); // 文件保存路径
if (!Directory.Exists(dirPath))
Directory.CreateDirectory(dirPath);
// 文件地址
string filePath = Path.Combine(dirPath, m.FileName);
if (!isSplitBlock)
{
if (File.Exists(filePath))
File.Delete(filePath);
}
int bufLen = 0;
byte[] buffer = new byte[4096];
using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAess.ReadWrite))
{
fs.Seek(0, SeekOrigin.End);
// 写入原文件
Stream sr = Context.Request.InputStream;
while ((bufLen = sr.Read(buffer, 0, buffer.Length)) > 0)
fs.Write(buffer, 0, bufLen);
sr.Close();
sr.Dispose();
// 缩略图
try
{
if (!m.NeedThumbnail)
return;
dirPath = Path.Combine(dirPath, "Small");
if (!Directory.Exists(dirPath))
Directory.CreateDirectory(dirPath);
filePath = Path.Combine(dirPath, m.FileName);
if (File.Exists(filePath))
File.Delete(filePath);
using (var pic = GetThumbnail(fs, 300, 300))
{
pic.Save(filePath);
}
}
catch { }
}
#endregion
#region 删除原文件
// 删除原文件
if (m.OldFilePath.IsNullOrEmpty())
{
return;
}
try
{
filePath = Path.Combine(ConfigHelper.UploadPath, m.OldFilePath);
if (File.Exists(filePath))
File.Delete(filePath);
if (m.NeedThumbnail)
{
filePath = Path.Combine(ConfigHelper.UploadPath, m.OldThumbnailImagePath);
if (File.Exists(filePath))
File.Delete(filePath);
}
}
catch (Exception ex)
{
}
#endregion
}
分块上传注意点每块流保存完以后再去读取下以块的数据,不然会多块一起过来会前面的块流数据会被后面的块流数据覆盖;
注重过程的注重结果
代码如下:
/// <summary>
/// 写入数据到流中
/// </summary>
/// <param name="url"></param>
/// <param name="callback"></param>
public async static Task<bool> Write(string url, Stream clientStream)
{
if (clientStream.Length > 2510241024)
url += "&t=1"; // 表示上传大文件
try
{
Up(url, clientStream);
return true;
}
catch { }
return false;
}
public async static Task Up(string url, Stream sourceStream)
{
var wc = new WebClient();
byte[] buffer = new byte[2510241024];
int bufLen = sourceStream.Read(buffer, 0, buffer.Length);
if (bufLen < 1)
{
sourceStream.Close();
return;
}
wc.WriteStreamClosed += (s, e) =>
{
if (sourceStream.CanRead)
Up(url, sourceStream);
else
sourceStream.Close();
};
var serverStream = await wc.OpenWriteTaskAsync(url, "POST");
serverStream.Write(buffer, 0, bufLen);
serverStream.Close();
}
服务端
代码如下:
private void Save()
{
string data = Context.Request.QueryString["data"].Base64StringDecode("ABC");
if (data.IsNullOrEmpty())
return;
var m = JsonConvert.DeserializeObject<FileUploadModel>(data);
if (m == null)
return;
var isSplitBlock = Context.Request.QueryString["t"]=="1"; //是否分块上传
#region 保存文件
// 初始化目录
string dirPath = Path.Combine(ConfigHelper.UploadPath, m.Dir); // 文件保存路径
if (!Directory.Exists(dirPath))
Directory.CreateDirectory(dirPath);
// 文件地址
string filePath = Path.Combine(dirPath, m.FileName);
if (!isSplitBlock)
{
if (File.Exists(filePath))
File.Delete(filePath);
}
int bufLen = 0;
byte[] buffer = new byte[4096];
using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAess.ReadWrite))
{
fs.Seek(0, SeekOrigin.End);
// 写入原文件
Stream sr = Context.Request.InputStream;
while ((bufLen = sr.Read(buffer, 0, buffer.Length)) > 0)
fs.Write(buffer, 0, bufLen);
sr.Close();
sr.Dispose();
// 缩略图
try
{
if (!m.NeedThumbnail)
return;
dirPath = Path.Combine(dirPath, "Small");
if (!Directory.Exists(dirPath))
Directory.CreateDirectory(dirPath);
filePath = Path.Combine(dirPath, m.FileName);
if (File.Exists(filePath))
File.Delete(filePath);
using (var pic = GetThumbnail(fs, 300, 300))
{
pic.Save(filePath);
}
}
catch { }
}
#endregion
#region 删除原文件
// 删除原文件
if (m.OldFilePath.IsNullOrEmpty())
{
return;
}
try
{
filePath = Path.Combine(ConfigHelper.UploadPath, m.OldFilePath);
if (File.Exists(filePath))
File.Delete(filePath);
if (m.NeedThumbnail)
{
filePath = Path.Combine(ConfigHelper.UploadPath, m.OldThumbnailImagePath);
if (File.Exists(filePath))
File.Delete(filePath);
}
}
catch (Exception ex)
{
}
#endregion
}
分块上传注意点每块流保存完以后再去读取下以块的数据,不然会多块一起过来会前面的块流数据会被后面的块流数据覆盖;
注重过程的注重结果
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程