Skip to content

Commit a1553e0

Browse files
authored
Merge pull request #122 from fengyhack/master
版本更新v7.2.4 部分细节优化与错误修正
2 parents 73078b6 + 0e59bba commit a1553e0

File tree

13 files changed

+571
-234
lines changed

13 files changed

+571
-234
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
**2017-01-12**
2+
3+
最新版本v7.2.4,适用于.NET Framework 2.0+ 和.NET Core
4+
5+
新增:分片上传(ResumableUploader)的uploadFile拥有更多的重载版本。
6+
7+
新增:上传文件支持用户自定义参数(extraParams)
8+
9+
* * *
10+
111
**2017-01-10**
212

313
最新版本v7.2.3,适用于.NET Framework 2.0+ 和.NET Core

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88

99
以下文档用于检索SDK接口、属性说明,它将有助于您理解SDK的结构。
1010

11-
* [HTML在线浏览](http://oiy037d6a.bkt.clouddn.com/csharp-sdk-ref-v7.2.3/index.html)
11+
* [HTML在线浏览](http://oiy037d6a.bkt.clouddn.com/csharp-sdk-ref-v7.2.4/index.html)
1212

13-
* [CHM文件下载](http://oiy037d6a.bkt.clouddn.com/QiniuCSharpSDK-Ref-v7.2.3.chm)
13+
* [CHM文件下载](http://oiy037d6a.bkt.clouddn.com/QiniuCSharpSDK-Ref-v7.2.4.chm)
1414

1515
以下文档/链接提供一些基本示例,参考这些示例可以帮助您更快熟悉如何使用这套SDK。
1616

1717
* [github | csharp-sdk-examples](https://github.com/fengyhack/csharp-sdk-examples)
1818

19-
* [C# SDK使用指南 | 代码示例](http://oiy037d6a.bkt.clouddn.com/csharp-sdk-man-v7.2.3/index.html)
19+
* [C# SDK使用指南 | 代码示例](http://oiy037d6a.bkt.clouddn.com/csharp-sdk-man-v7.2.4/index.html)
2020

2121
##如何安装
2222

doc/README.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ <h2 id="sdk文档">SDK文档</h2>
1717

1818
<p>以下文档用于检索SDK接口、属性说明,它将有助于您理解SDK的结构。</p>
1919

20-
<ul><li><p><a href="http://oiy037d6a.bkt.clouddn.com/csharp-sdk-ref-v7.2.2/index.html" target="_blank">HTML在线浏览</a></p></li>
21-
<li><p><a href="http://oiy037d6a.bkt.clouddn.com/QiniuCSharpSDK-Ref-v7.2.2.chm" target="_blank">CHM文件下载</a></p></li>
20+
<ul><li><p><a href="http://oiy037d6a.bkt.clouddn.com/csharp-sdk-ref-v7.2.4/index.html" target="_blank">HTML在线浏览</a></p></li>
21+
<li><p><a href="http://oiy037d6a.bkt.clouddn.com/QiniuCSharpSDK-Ref-v7.2.4.chm" target="_blank">CHM文件下载</a></p></li>
2222
</ul>
2323

2424
<p>以下文档/链接提供一些基本示例,参考这些示例可以帮助您更快熟悉如何使用这套SDK。</p>
2525

2626
<ul><li><p><a href="https://github.com/fengyhack/csharp-sdk-shared-examples" target="_blank">github | csharp-sdk-examples</a></p></li>
27-
<li><p><a href="http://oiy037d6a.bkt.clouddn.com/csharp-sdk-man-v7.2.2/index.html" target="_blank">C# SDK使用指南 | 代码示例</a></p></li>
27+
<li><p><a href="http://oiy037d6a.bkt.clouddn.com/csharp-sdk-man-v7.2.4/index.html" target="_blank">C# SDK使用指南 | 代码示例</a></p></li>
2828
</ul>
2929

3030

doc/README.pdf

1.83 KB
Binary file not shown.

src/Qiniu/Common/QiniuCSharpSDK.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class QiniuCSharpSDK
1111
/// <summary>
1212
/// SDK版本号
1313
/// </summary>
14-
public const string VERSION = "7.2.2.0";
14+
public const string VERSION = "7.2.4";
1515

1616
/// <summary>
1717
/// SDK模块

src/Qiniu/Http/HttpManager.cs

Lines changed: 16 additions & 103 deletions
Large diffs are not rendered by default.

src/Qiniu/IO/DownloadManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public HttpResult downloadPriv(string signedUrl,string saveasFile)
7272

7373
try
7474
{
75-
result = httpManager.get(signedUrl, null, null, true);
75+
result = httpManager.get(signedUrl, null, true);
7676
if (result.Code == HttpHelper.STATUS_CODE_OK)
7777
{
7878
using (FileStream fs = File.Create(saveasFile, result.Data.Length))
@@ -121,7 +121,7 @@ public static HttpResult download(string url,string saveasFile)
121121
{
122122
HttpManager httpManager = new HttpManager();
123123

124-
result = httpManager.get(url, null, null, true);
124+
result = httpManager.get(url, null, true);
125125
if (result.Code == HttpHelper.STATUS_CODE_OK)
126126
{
127127
using (FileStream fs = File.Create(saveasFile, result.Data.Length))

src/Qiniu/IO/ResumableUploader.cs

Lines changed: 383 additions & 91 deletions
Large diffs are not rendered by default.

src/Qiniu/IO/SimpleUploader.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.IO;
33
using System.Text;
4+
using System.Collections.Generic;
45
using Qiniu.Common;
56
using Qiniu.Http;
67

@@ -98,6 +99,76 @@ public HttpResult uploadFile(string localFile, string saveKey, string token)
9899
return result;
99100
}
100101

102+
/// <summary>
103+
/// 上传文件 - 可附加自定义参数
104+
/// </summary>
105+
/// <param name="localFile">待上传的本地文件</param>
106+
/// <param name="saveKey">要保存的目标文件名称</param>
107+
/// <param name="token">上传凭证</param>
108+
/// <param name="extraParams">用户自定义的附加参数</param>
109+
/// <returns></returns>
110+
public HttpResult uploadFile(string localFile, string saveKey, string token, Dictionary<string,string> extraParams)
111+
{
112+
HttpResult result = new HttpResult();
113+
114+
try
115+
{
116+
string boundary = HttpHelper.createFormDataBoundary();
117+
string sep = "--" + boundary;
118+
StringBuilder sb = new StringBuilder();
119+
120+
sb.AppendLine(sep);
121+
122+
sb.AppendLine("Content-Disposition: form-data; name=key\r\n");
123+
sb.AppendLine(saveKey);
124+
sb.AppendLine(sep);
125+
126+
sb.AppendLine("Content-Disposition: form-data; name=token\r\n");
127+
sb.AppendLine(token);
128+
sb.AppendLine(sep);
129+
130+
foreach (var d in extraParams)
131+
{
132+
sb.AppendFormat("Content-Disposition: form-data; name=\"{0}\"\r\n\r\n", d.Key);
133+
sb.AppendLine(d.Value);
134+
sb.AppendLine(sep);
135+
}
136+
137+
string filename = Path.GetFileName(localFile);
138+
sb.AppendFormat("Content-Disposition: form-data; name=file; filename={0}\r\n\r\n", filename);
139+
140+
byte[] partData1 = Encoding.UTF8.GetBytes(sb.ToString());
141+
byte[] partData2 = File.ReadAllBytes(localFile);
142+
byte[] partData3 = Encoding.UTF8.GetBytes(string.Format("\r\n{0}--\r\n", sep));
143+
144+
MemoryStream ms = new MemoryStream();
145+
ms.Write(partData1, 0, partData1.Length);
146+
ms.Write(partData2, 0, partData2.Length);
147+
ms.Write(partData3, 0, partData3.Length);
148+
149+
result = httpManager.postMultipart(uploadHost, ms.ToArray(), boundary, null);
150+
result.RefText += string.Format("[SimpleUpload] Uploaded: \"{0}\" ==> \"{1}\", @{2}\n",
151+
localFile, saveKey, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff"));
152+
}
153+
catch (Exception ex)
154+
{
155+
StringBuilder sb = new StringBuilder("[SimpleUpload] Error: ");
156+
Exception e = ex;
157+
while (e != null)
158+
{
159+
sb.Append(e.Message + " ");
160+
e = e.InnerException;
161+
}
162+
163+
sb.AppendFormat(" @{0}\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff"));
164+
165+
result.RefCode = HttpHelper.STATUS_CODE_EXCEPTION;
166+
result.RefText += sb.ToString();
167+
}
168+
169+
return result;
170+
}
171+
101172
/// <summary>
102173
/// 上传数据流
103174
/// </summary>

src/Qiniu/IO/UploadManager.cs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ public class UploadManager
2525
// 是否从CDN上传
2626
private bool UPLOAD_FROM_CDN = false;
2727

28+
// 上传进度处理器 - 仅用于上传大文件
29+
private UploadProgressHandler upph = null;
30+
31+
// 上传控制器 - 仅用于上传大文件
32+
private UploadController upctl = null;
33+
34+
// 上传记录文件 - 仅用于上传大文件
35+
private string recordFile = null;
36+
2837
/// <summary>
2938
/// 初始化
3039
/// </summary>
@@ -36,6 +45,33 @@ public UploadManager(long putThreshold = 1048576, bool uploadFromCDN = false)
3645
UPLOAD_FROM_CDN = uploadFromCDN;
3746
}
3847

48+
/// <summary>
49+
/// 设置上传进度处理器-仅对于上传大文件有效
50+
/// </summary>
51+
/// <param name="upph">上传进度处理器</param>
52+
public void setUploadProgressHandler(UploadProgressHandler upph)
53+
{
54+
this.upph = upph;
55+
}
56+
57+
/// <summary>
58+
/// 设置上传控制器-仅对于上传大文件有效
59+
/// </summary>
60+
/// <param name="upctl">上传控制器</param>
61+
public void setUploadController(UploadController upctl)
62+
{
63+
this.upctl = upctl;
64+
}
65+
66+
/// <summary>
67+
/// 设置断点记录文件-仅对于上传大文件有效
68+
/// </summary>
69+
/// <param name="recordFile">记录文件</param>
70+
public void setRecordFile(string recordFile)
71+
{
72+
this.recordFile = recordFile;
73+
}
74+
3975
/// <summary>
4076
/// 设置分片上传的“片”大小(单位:字节)
4177
/// </summary>
@@ -73,8 +109,23 @@ public HttpResult uploadFile(string localFile, string saveKey, string token)
73109
FileInfo fi = new FileInfo(localFile);
74110
if (fi.Length > PUT_THRESHOLD)
75111
{
76-
ResumableUploader ru = new ResumableUploader(UPLOAD_FROM_CDN);
77-
result = ru.uploadFile(localFile, saveKey, token);
112+
if (string.IsNullOrEmpty(recordFile))
113+
{
114+
recordFile = "QiniuRU_" + Util.StringHelper.calcMD5(localFile + saveKey);
115+
}
116+
117+
if (upph == null)
118+
{
119+
upph = new UploadProgressHandler(ResumableUploader.defaultUploadProgressHandler);
120+
}
121+
122+
if (upctl == null)
123+
{
124+
upctl = new UploadController(ResumableUploader.defaultUploadController);
125+
}
126+
127+
ResumableUploader ru = new ResumableUploader(UPLOAD_FROM_CDN, CHUNK_UNIT);
128+
result = ru.uploadFile(localFile, saveKey, token, recordFile, upph, upctl);
78129
}
79130
else
80131
{

0 commit comments

Comments
 (0)