Skip to content

Commit 7d7fb74

Browse files
committed
fix
1 parent 953bead commit 7d7fb74

File tree

6 files changed

+129
-2
lines changed

6 files changed

+129
-2
lines changed

demo/Program.cs

+29-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ public static void Main (string[] args)
2222
string localfile = "/Users/icattlecoder/Movies/tzd.rmvb";
2323
string bucket = "icattlecoder";
2424
string qiniukey = "tzd.rmvb";
25-
25+
26+
{
27+
UploadBase64 ();
28+
}
29+
2630
//======================================================================
2731
{
2832
QiniuFile qfile = new QiniuFile (bucket, qiniukey, localfile);
@@ -78,5 +82,29 @@ public static void Main (string[] args)
7882
*/
7983
}
8084
}
85+
86+
/// <summary>
87+
/// Uploads the base64.
88+
/// </summary>
89+
public static void UploadBase64(){
90+
string bucket = "icattlecoder";
91+
string qiniuKey = "base64.png";
92+
93+
ManualResetEvent done = new ManualResetEvent (false);
94+
jpegToBase64 jpeg = new jpegToBase64 ("/Users/icattlecoder/Desktop/base64.png");
95+
QiniuFile qfile = new QiniuFile (bucket, qiniuKey);
96+
qfile.UploadCompleted+= (sender, e) => {
97+
Console.Write(e.RawString);
98+
done.Set();
99+
100+
};
101+
qfile.UploadFailed+= (sender, e) => {
102+
QiniuWebException qe = (QiniuWebException)e.Error;
103+
Console.WriteLine(qe.Error.ToString());
104+
};
105+
qfile.UploadString ((int)jpeg.Filesize, "image/png", jpeg.Base64Content);
106+
done.WaitOne ();
107+
}
108+
81109
}
82110
}

demo/demo.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<ItemGroup>
3535
<Compile Include="Program.cs" />
3636
<Compile Include="Properties\AssemblyInfo.cs" />
37+
<Compile Include="jpegToBase64.cs" />
3738
</ItemGroup>
3839
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
3940
<ItemGroup>

demo/jpegToBase64.cs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.IO;
3+
4+
namespace demo
5+
{
6+
public class jpegToBase64
7+
{
8+
string filename;
9+
long filesize;
10+
string base64Content;
11+
12+
public string Filename {
13+
get { return filename; }
14+
}
15+
16+
public long Filesize {
17+
get { return filesize; }
18+
}
19+
20+
public string Base64Content {
21+
get { return base64Content; }
22+
}
23+
24+
public jpegToBase64 (string filename)
25+
{
26+
if (!File.Exists (filename)) {
27+
throw new ArgumentException (filename + " not exists");
28+
}
29+
this.filename = filename;
30+
FileInfo finfo = new FileInfo (filename);
31+
this.filesize = finfo.Length;
32+
using (FileStream fs = finfo.OpenRead ()) {
33+
byte[] content = new byte[this.filesize];
34+
fs.Read (content, 0, (int)this.filesize);
35+
this.base64Content = Convert.ToBase64String (content);
36+
}
37+
}
38+
}
39+
}
40+

sdk/QiniuEventArgs.cs

+11
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@ public QiniuUploadCompletedEventArgs(byte[] result){
7272
}
7373
}
7474
}
75+
76+
public QiniuUploadCompletedEventArgs(string result){
77+
this.RawString = result;
78+
Dictionary<string, object> dict = JsonConvert.DeserializeObject<Dictionary<string, object>> (result);
79+
object tmp;
80+
if (dict.TryGetValue ("hash", out tmp))
81+
Hash = (string)tmp;
82+
if (dict.TryGetValue ("key", out tmp))
83+
key = (string)tmp;
84+
85+
}
7586
}
7687

7788
/// <summary>

sdk/QiniuFile.cs

+47
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,53 @@ public bool Move(string destBucket,string destKey,MAC mac=null){
184184
}
185185
}
186186

187+
/// <summary>
188+
/// Uploads the string.
189+
/// </summary>
190+
/// <param name="base64Content">Base64 content.</param>
191+
public void UploadString(int filesize,string mimeType,string base64Content){
192+
string token = getDefalutToken (this.bucketName, this.key);
193+
UploadString (token, filesize, mimeType, base64Content);
194+
}
195+
196+
/// <summary>
197+
/// Uploads the string.
198+
/// </summary>
199+
/// <param name="token">Token.</param>
200+
/// <param name="base64Content">Base64 content.</param>
201+
public void UploadString(string token,int fileSize,string mimeType,string base64Content){
202+
using (QiniuWebClient qwc = new QiniuWebClient ()) {
203+
qwc.UpToken = token;
204+
string url = Config.UP_HOST +
205+
string.Format("/putb64/{0}/key/{1}/mimeType/{2}",
206+
fileSize,
207+
Base64URLSafe.Encode(this.key),
208+
Base64URLSafe.Encode(mimeType));
209+
210+
qwc.UploadStringCompleted += (sender, e) => {
211+
if (e.Error != null && e.Error is WebException) {
212+
if (e.Error is WebException) {
213+
QiniuWebException qwe = new QiniuWebException (e.Error as WebException);
214+
onUploadFailed (new QiniuUploadFailedEventArgs (qwe));
215+
} else {
216+
onUploadFailed (new QiniuUploadFailedEventArgs (e.Error));
217+
}
218+
} else {
219+
onQiniuUploadCompleted(new QiniuUploadCompletedEventArgs(e.Result));
220+
221+
onQiniuUploadCompleted (new QiniuUploadCompletedEventArgs (e.Result));
222+
}
223+
};
224+
225+
qwc.UploadProgressChanged += (sender, e) => {
226+
onQiniuUploadProgressChanged (new QiniuUploadProgressChangedEventArgs (e.BytesSent, e.TotalBytesToSend));
227+
};
228+
229+
qwc.Headers.Add("Content-Type", "application/octet-stream");
230+
qwc.UploadStringAsync (new Uri (url), "POST", base64Content);
231+
}
232+
}
233+
187234
/// <summary>
188235
/// Asyncs the upload.
189236
/// </summary>

sdk/qiniu.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
<Reference Include="System.Configuration" />
3434
<Reference Include="System.Configuration.Install" />
3535
<Reference Include="Newtonsoft.Json">
36-
<HintPath>..\bin\Debug\Newtonsoft.Json.dll</HintPath>
36+
<HintPath>bin\Debug\Newtonsoft.Json.dll</HintPath>
3737
</Reference>
3838
</ItemGroup>
3939
<ItemGroup>

0 commit comments

Comments
 (0)