silverlight用webclient大文件上傳的實(shí)例代碼
更新時(shí)間:2013年10月14日 14:46:52 作者:
這篇文章介紹了silverlight用webclient大文件上傳的實(shí)例代碼,有需要的朋友可以參考一下
客戶(hù)端:
/// <summary>
/// 寫(xiě)入數(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);
// 寫(xiě)入原文件
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
}
分塊上傳注意點(diǎn):每塊流保存完以后再去讀取下以塊的數(shù)據(jù),不然會(huì)多塊一起過(guò)來(lái)會(huì)前面的塊流數(shù)據(jù)會(huì)被后面的塊流數(shù)據(jù)覆蓋;
注重過(guò)程的同時(shí)注重結(jié)果
復(fù)制代碼 代碼如下:
/// <summary>
/// 寫(xiě)入數(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);
// 寫(xiě)入原文件
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
}
分塊上傳注意點(diǎn):每塊流保存完以后再去讀取下以塊的數(shù)據(jù),不然會(huì)多塊一起過(guò)來(lái)會(huì)前面的塊流數(shù)據(jù)會(huì)被后面的塊流數(shù)據(jù)覆蓋;
注重過(guò)程的同時(shí)注重結(jié)果
相關(guān)文章
LiteralControl ASP.NET中的另類(lèi)控件
對(duì)于LiteralControl控件的應(yīng)用比較少,今天突然看到了,就弄個(gè)明白為好,所以總結(jié)出來(lái),供大家一起學(xué)習(xí)2012-01-01C#默認(rèn)以管理員身份運(yùn)行程序?qū)崿F(xiàn)代碼
權(quán)限不夠,導(dǎo)致無(wú)法修改系統(tǒng)時(shí)間,于是我以管理員身份運(yùn)行了一次,結(jié)果測(cè)試成功,下面為大家介紹下C#如何默認(rèn)以管理員身份運(yùn)行程序2014-03-03ASP.NET Global.asax應(yīng)用程序文件簡(jiǎn)介
Global.asax 文件,有時(shí)候叫做 ASP.NET 應(yīng)用程序文件,提供了一種在一個(gè)中心位置響應(yīng)應(yīng)用程序級(jí)或模塊級(jí)事件的方法。2009-03-03ASP.NET?MVC使用Identity增刪改查用戶(hù)
這篇文章介紹了ASP.NET?MVC使用Identity增刪改查用戶(hù)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10IdentityServer4實(shí)現(xiàn).Net Core API接口權(quán)限認(rèn)證(快速入門(mén))
這篇文章主要介紹了IdentityServer4實(shí)現(xiàn).Net Core API接口權(quán)限認(rèn)證,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03Excel、記事本數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法
將手機(jī)號(hào)批量導(dǎo)入數(shù)據(jù)庫(kù)。思路:先將要導(dǎo)入的文件傳上項(xiàng)目里,然后讀取文件的每行數(shù)據(jù)并插入數(shù)據(jù)庫(kù),操作完后再將上傳的文件刪除2013-10-10