silverlight用webclient大文件上傳的實例代碼
更新時間:2013年10月14日 14:46:52 作者:
這篇文章介紹了silverlight用webclient大文件上傳的實例代碼,有需要的朋友可以參考一下
客戶端:
/// <summary>
/// 寫入數(shù)據(jù)到流中
/// </summary>
/// <param name="url"></param>
/// <param name="callback"></param>
public async static Task<bool> Write(string url, Stream clientStream)
{
if (clientStream.Length > 25*1024*1024)
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[25*1024*1024];
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();
}
服務(wù)端:
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, FileAccess.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
}
分塊上傳注意點:每塊流保存完以后再去讀取下以塊的數(shù)據(jù),不然會多塊一起過來會前面的塊流數(shù)據(jù)會被后面的塊流數(shù)據(jù)覆蓋;
注重過程的同時注重結(jié)果
復(fù)制代碼 代碼如下:
/// <summary>
/// 寫入數(shù)據(jù)到流中
/// </summary>
/// <param name="url"></param>
/// <param name="callback"></param>
public async static Task<bool> Write(string url, Stream clientStream)
{
if (clientStream.Length > 25*1024*1024)
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[25*1024*1024];
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();
}
服務(wù)端:
復(fù)制代碼 代碼如下:
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, FileAccess.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
}
分塊上傳注意點:每塊流保存完以后再去讀取下以塊的數(shù)據(jù),不然會多塊一起過來會前面的塊流數(shù)據(jù)會被后面的塊流數(shù)據(jù)覆蓋;
注重過程的同時注重結(jié)果
相關(guān)文章
ASP.NET Global.asax應(yīng)用程序文件簡介
Global.asax 文件,有時候叫做 ASP.NET 應(yīng)用程序文件,提供了一種在一個中心位置響應(yīng)應(yīng)用程序級或模塊級事件的方法。2009-03-03IdentityServer4實現(xiàn).Net Core API接口權(quán)限認證(快速入門)
這篇文章主要介紹了IdentityServer4實現(xiàn).Net Core API接口權(quán)限認證,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03Excel、記事本數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫的實現(xiàn)方法
將手機號批量導(dǎo)入數(shù)據(jù)庫。思路:先將要導(dǎo)入的文件傳上項目里,然后讀取文件的每行數(shù)據(jù)并插入數(shù)據(jù)庫,操作完后再將上傳的文件刪除2013-10-10