Skip to content

Commit 6032af2

Browse files
author
fengyunhai
committed
更新示例代码
新增一个UploadStream示例
1 parent be30540 commit 6032af2

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

examples/Examples/App.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ static void Main(string[] args)
2222
//// 上传数据流
2323
//UploadDemo.uploadStream();
2424

25+
//// 上传数据流(NetStream)
26+
//UploadDemo.uploadNetStream();
27+
2528
//// 上传后触发fop(数据处理)
2629
//UploadDemo.uploadWithFop();
2730

examples/Examples/IO.Examples.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,38 @@ public static void uploadStream()
140140
Console.WriteLine(result);
141141
}
142142

143+
/// <summary>
144+
/// 上传数据流(NetStream)
145+
/// </summary>
146+
public static void uploadNetStream()
147+
{
148+
try
149+
{
150+
string url = "http://img.ivsky.com/img/tupian/pre/201610/09/beifang_shanlin_xuejing-001.jpg";
151+
var wReq = System.Net.WebRequest.Create(url) as System.Net.HttpWebRequest;
152+
var resp = wReq.GetResponse() as System.Net.HttpWebResponse;
153+
using (var stream = resp.GetResponseStream())
154+
{
155+
Mac mac = new Mac(Settings.AccessKey, Settings.SecretKey);
156+
PutPolicy putPolicy = new PutPolicy();
157+
putPolicy.Scope = "xuejing-001.jpg";
158+
putPolicy.SetExpires(3600);
159+
string jstr = putPolicy.ToJsonString();
160+
string token = Auth.CreateUploadToken(mac, jstr);
161+
162+
// 请不要使用UploadManager的UploadStream方法,因为此流不支持查找(无法获取Stream.Length)
163+
// 请使用FormUploader或者ResumableUploader的UploadStream方法
164+
FormUploader fu = new FormUploader();
165+
var result = fu.UploadStream(stream, "xuejing-001.jpg", token);
166+
Console.WriteLine(result);
167+
}
168+
}
169+
catch(Exception ex)
170+
{
171+
Console.WriteLine(ex);
172+
}
173+
}
174+
143175
/// <summary>
144176
/// 上传完毕后触发数据处理
145177
/// </summary>

0 commit comments

Comments
 (0)