先说一下原理
在阿里云 net.sdk 中 关于上传文件有这样的示例:
OssClient ossClient = new OssClient(accessId, accessKey);
using(var fs = File.Open(fileToUpload, FileMode.Open))
{
ossClient.PutObject(bucketName, key, fs, metadata);
}
这是上传一个文件,文件大小小于5G。
其中 fs 可以替换成Stream ,这样就可以读取到上传的进度了
private Stream fs;
private void bgw_DoWork(object sender, DoWorkEventArgs e)
{
fs = File.Open(FileToUpload, FileMode.Open);
ObjectMetadata metadata = new ObjectMetadata();
ClientConfiguration config = new ClientConfiguration();
config.ConnectionTimeout = 3600 * 1000; // 这里必须设置一下,不然会超时导致上传失败,在上论坛找到的
OssClient ossClient = new OssClient(new Uri(AliyunHelper.SDK.OSSUrl), AccessID, AccessKey, config);
PutObjectResult Result = ossClient.PutObject(Bucket, Key, fs, metadata);
}
然后 需要读取上传进度的时候 计算一下就可以了
/// <summary>
/// 计算上传速度和进度
/// </summary>
public void GetSpeed()
{
if (UploadFilish)
{
NeedTime = new TimeSpan(0, 0, 0);
Speed = 0;
Process = 100;
return;
}
try
{
long Position = 0;
if (fs != null)
{
Position = fs.Position;
}
long UploadNum = Position - UploadSize;//两次之间的差值
Speed = (int)(UploadNum / 1024);// KB/s
UploadSize = Position;//更新所在位置
if (FileSize > 0)
{
Process = (int)(UploadSize * 100 / FileSize);//计算进度 %
}
long LastSize = FileSize - UploadSize;
int UseTime = 0;
if (Speed > 0)
{
UseTime = (int)(LastSize / 1024 / Speed);//需要多少秒
}
NeedTime = new TimeSpan(0, 0, UseTime);
}
catch
{ }
if (UploadFilish)
{
NeedTime = new TimeSpan(0, 0, 0);
Speed = 0;
Process = 100;
}
}
具体的 可以在界面上拖一个Timer控件,间隔设为1秒
private void timerUpload_Tick(object sender, EventArgs e)
{
int Total = 0;
int Num = 0;
foreach (DataGridViewRow row in dataUpload.Rows)
{
string State = row.Cells["State"].Value.ToString();
if (State == "等待上传")
{
Num ;
string FileToUpload = row.Cells["FileName"].Value.ToString();
foreach (var item in ListUpload)
{
if (item.FileToUpload == FileToUpload)
{
row.Cells["State"].Value = item.State;
}
}
}
else if (State == "正在上传")
{
Num ;
string FileToUpload = row.Cells["FileName"].Value.ToString();
foreach (var item in ListUpload)
{
if (item.FileToUpload == FileToUpload)
{
if (item.UploadFilish)
{
row.Cells["State"].Value = item.State;
row.Cells["Process"].Value = 100;
row.Cells["Speed"].Value = 0;
row.Cells["NeedTime"].Value = "00:00:00";
}
else
{
item.GetSpeed();//在这里 调用计算,下面紧接着就是 显示计算结果
row.Cells["Process"].Value = item.Process;
int Speed = item.Speed;
Total = Speed;
row.Cells["Speed"].Value = Speed.ToString();
row.Cells["NeedTime"].Value = item.NeedTime.ToString();
}
}
}
}
else
{
}
}
lblUploadWaiteNum.Text = Num.ToString();
lblSpeedTotal.Text = Total.ToString();
}
最后 附上完整的类代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aliyun.OpenServices.OpenStorageService;
using System.IO;
using System.ComponentModel;
using System.Timers;
using System.Threading;
using Aliyun.OpenServices;
using System.Security.Policy;
namespace ChangDu.AliyunHelper
{
public class Upload
{
/// <summary>
/// 要上传的文件=Folder "\\" Key
/// </summary>
/// <param name="AccessID"></param>
/// <param name="AccessKey"></param>
/// <param name="Bucket"></param>
/// <param name="Key"></param>
/// <param name="Folder"></param>
public Upload(string AccessID, string AccessKey, string Bucket, string Key, string Folder)
{
this.AccessID = AccessID;
this.AccessKey = AccessKey;
this.Bucket = Bucket;
this.Key = Key;
this.FileToUpload = Folder "\\" Key;
this.FileToMove = Folder "\\Upload\\" Key;
FileSize = new FileInfo(FileToUpload).Length;
UploadFilish = false;
UploadOK = false;
UploadSize = 0;
Process = 0;
Speed = 0;
UploadStart = false;
Started = false;
State = "等待上传";
}
public string AccessID { get; set; }
public string AccessKey { get; set; }
public string Bucket { get; set; }
/// <summary>
/// 保存到OSS的KEY
/// </summary>
public string Key { get; set; }
/// <summary>
/// 要上传的文件的本地路径
/// </summary>
public string FileToUpload { get; set; }
private string FileToMove { get; set; }
/// <summary>
/// 上传进度 百分比
/// </summary>
public int Process { get; set; }
/// <summary>
/// 上传速度 KB/s
/// </summary>
public int Speed { get; set; }
public TimeSpan NeedTime { get; set; }
/// <summary>
/// 是否已经开始上传
/// </summary>
public bool UploadStart { get; set; }
public bool Started { get; set; }
/// <summary>
/// 上传是否已经完成
/// </summary>
public bool UploadFilish { get; set; }
/// <summary>
/// 上传结果 是否成功
/// </summary>
public bool UploadOK { get; set; }
private Stream fs;
private string MD5 = "";
/// <summary>
/// 要上传的文件大小
/// </summary>
public long FileSize = 0;
private long UploadSize = 0;
private string _State = "";
/// <summary>
/// 上传状态
/// </summary>
public string State
{
get
{
lock (this)
{
return _State;
}
}
set
{
lock (this)
{
_State = value;
}
}
}
/// <summary>
/// 正式开始上传
/// </summary>
public void StartUpload()
{
Started = true;
State = "正在上传";
BackgroundWorker bgw = new BackgroundWorker();
bgw.DoWork = bgw_DoWork;
bgw.RunWorkerCompleted = bgw_RunWorkerCompleted;
bgw.RunWorkerAsync();
}
/// <summary>
/// 计算上传速度和进度
/// </summary>
public void GetSpeed()
{
if (UploadFilish)
{
NeedTime = new TimeSpan(0, 0, 0);
Speed = 0;
Process = 100;
return;
}
try
{
long Position = 0;
if (fs != null)
{
Position = fs.Position;
}
long UploadNum = Position - UploadSize;//两次之间的差值
Speed = (int)(UploadNum / 1024);// KB/s
UploadSize = Position;//更新所在位置
if (FileSize > 0)
{
Process = (int)(UploadSize * 100 / FileSize);//计算进度 %
}
long LastSize = FileSize - UploadSize;
int UseTime = 0;
if (Speed > 0)
{
UseTime = (int)(LastSize / 1024 / Speed);//需要多少秒
}
NeedTime = new TimeSpan(0, 0, UseTime);
}
catch
{ }
if (UploadFilish)
{
NeedTime = new TimeSpan(0, 0, 0);
Speed = 0;
Process = 100;
}
}
/// <summary>
/// 上传结束
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
try
{
if (fs != null)
{
fs.Close();
fs.Dispose();
}
UploadFilish = true;
}
catch
{ }
finally
{
}
}
/// <summary>
/// 上传中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void bgw_DoWork(object sender, DoWorkEventArgs e)
{
try
{
//申请许可证
seamphore.WaitOne();
UploadStart = true;
MD5 = MD5Helper.GetMd5Hash(FileToUpload);
fs = File.Open(FileToUpload, FileMode.Open);
ObjectMetadata metadata = new ObjectMetadata();
// 可以设定自定义的metadata。
metadata.ContentType = "application/pdf";
ClientConfiguration config = new ClientConfiguration();
config.ConnectionTimeout = 3600 * 1000; // 根据用户情况设置 (这里也可以设置成 -1 表示永不超时)
OssClient ossClient = new OssClient(new Uri(AliyunHelper.SDK.OSSUrl), AccessID, AccessKey, config);
PutObjectResult Result = ossClient.PutObject(Bucket, Key, fs, metadata);
if (Result.ETag == MD5)
{
//上传成功
UploadOK = true;
State = "上传成功";
try
{
if (File.Exists(FileToMove))
{
string Dir = Path.GetDirectoryName(FileToMove);
if (!Directory.Exists(Dir))
{
Directory.CreateDirectory(Dir);
}
File.Move(FileToUpload, FileToMove);
}
}
catch
{ }
}
else
{
UploadOK = false;
State = "上传失败";
SDK.DeleteObject(ossClient, Bucket, Key);
}
}
catch (Exception ex)
{
//请求被中止: 请求已被取消
UploadOK = false;
State = "上传失败";
}
finally
{
//释放许可证
seamphore.Release();
}
}
public static int MaxThreadNum = 3;
public static Semaphore seamphore = new Semaphore(MaxThreadNum, MaxThreadNum);
/// <summary>
/// 设置 最大的同时上传线程数
/// </summary>
/// <param name="MaxNum"></param>
public static void SetThreadNum(int MaxNum)
{
MaxThreadNum = MaxNum;
seamphore = new Semaphore(MaxNum, MaxNum);
}
}
}
-------------------------
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。