forked from thinkry/wecode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpLoad.cs
269 lines (235 loc) · 10.2 KB
/
UpLoad.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Configuration;
using Newtonsoft.Json.Linq;
using System.Threading;
using System.Data.OleDb;
namespace WeCode1._0
{
public partial class UpLoad : Form
{
Thread t;
private string _URL;
private string _Filename;
private string _Path;
private string _length;
private string _notepath;
public UpLoad(string URL, string Path,string FileName,string NotePath)
{
InitializeComponent();
this._URL = URL;
this._Filename = FileName;
this._Path = Path;
this._notepath = NotePath;
}
private void UpLoad_Load(object sender, EventArgs e)
{
}
private void UpLoad_Shown(object sender, EventArgs e)
{
//新线程下载
Control.CheckForIllegalCrossThreadCalls = false;
t = new Thread(new ThreadStart(ThFun));
t.Start();
}
public void ThFun()
{
try
{
string JRtn = Upload_Request(_URL, _Path, _Filename, this.progressBar1);
if (JRtn == "")
{
MessageBox.Show("上传失败!");
return;
}
else
{
JObject jo = JObject.Parse(JRtn);
string fileUrl = jo["url"].ToString();
//更新笔记
NoteAPI.UpdateRource(fileUrl, _length, _Filename, _notepath);
//更新缓存数据
OleDbConnection ExportConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+PubFunc.GetYoudaoDBPath());
FileStream fs = new FileStream(_Path, FileMode.Open, FileAccess.Read);
byte[] data = new byte[fs.Length];
fs.Read(data, 0, (int)fs.Length);
int Datalength = (int)fs.Length;
fs.Close();
string NewAffixid = AccessAdo.ExecuteScalar(ExportConn,"select max(affixid) from tattachment").ToString();
int intNewAffixid = NewAffixid == "" ? 1 : Convert.ToInt32(NewAffixid) + 1;
int Nodeid = (int)AccessAdo.ExecuteScalar(ExportConn, "select nodeid from ttree where path='" + _notepath + "'");
string Title = _Filename;
int timeSeconds = PubFunc.time2TotalSeconds();
//affixid
OleDbParameter p1 = new OleDbParameter("@affixid", OleDbType.Integer);
p1.Value = intNewAffixid;
//nodeid
OleDbParameter p2 = new OleDbParameter("@nodeid", OleDbType.Integer);
p2.Value = Nodeid;
//title
OleDbParameter p3 = new OleDbParameter("@title", OleDbType.VarChar);
p3.Value = Title;
//二进制数据
OleDbParameter p4 = new OleDbParameter("@Data", OleDbType.Binary);
p4.Value = data;
//size
OleDbParameter p5 = new OleDbParameter("@size", OleDbType.Integer);
p5.Value = Datalength;
//time
OleDbParameter p6 = new OleDbParameter("@time", OleDbType.Integer);
p6.Value = timeSeconds;
OleDbParameter p7 = new OleDbParameter("@gid", OleDbType.VarChar);
p7.Value = AccessAdo.ExecuteScalar(ExportConn,"select gid from ttree where path='"+_notepath+"'").ToString();
OleDbParameter[] arrPara = new OleDbParameter[7];
arrPara[0] = p1;
arrPara[1] = p2;
arrPara[2] = p3;
arrPara[3] = p4;
arrPara[4] = p5;
arrPara[5] = p6;
arrPara[6] = p7;
string SQL = "insert into tattachment([affixId],[NodeId],[Title],[Data],[Size],[Time],[Gid]) values(@affixid,@nodeid,@title,@Data,@size,@time,@gid)";
AccessAdo.ExecuteNonQuery(ExportConn,SQL, arrPara);
MessageBox.Show("上传成功!");
Attachment.AttForm.ReFreshAttachGrid();
this.Close();
}
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
}
}
////ceshi
// <summary>
/// 将本地文件上传到指定的服务器(HttpWebRequest方法)
/// </summary>
/// <param name="address">文件上传到的服务器</param>
/// <param name="fileNamePath">要上传的本地文件(全路径)</param>
/// <param name="saveName">文件上传后的名称</param>
/// <param name="progressBar">上传进度条</param>
/// <returns>成功返回Json,失败返回""</returns>
private string Upload_Request(string address, string fileNamePath, string saveName, ProgressBar progressBar)
{
string returnValue = "";
// 要上传的文件
FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
//时间戳
string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x");
byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + strBoundary + "--\r\n");
//请求头部信息
StringBuilder sb = new StringBuilder();
sb.Append("--");
sb.Append(strBoundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"");
sb.Append("file");
sb.Append("\"; filename=\"");
sb.Append(saveName);
sb.Append("\"");
sb.Append("\r\n");
sb.Append("Content-Type: ");
sb.Append("application/octet-stream");
sb.Append("\r\n");
sb.Append("\r\n");
string strPostHeader = sb.ToString();
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(strPostHeader);
// 根据uri创建HttpWebRequest对象
HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(new Uri(address));
httpReq.Method = "POST";
httpReq.Headers.Add("Authorization", "OAuth oauth_token=\"" + ConfigurationManager.AppSettings["AccessToken"] + "\"");
//对发送的数据不使用缓存
httpReq.AllowWriteStreamBuffering = false;
//设置获得响应的超时时间(300秒)
httpReq.Timeout = 300000;
httpReq.ContentType = "multipart/form-data; boundary=" + strBoundary;
long length = fs.Length + postHeaderBytes.Length + boundaryBytes.Length;
long fileLength = fs.Length;
_length = fileLength.ToString();
httpReq.ContentLength = length;
try
{
progressBar.Maximum = int.MaxValue;
progressBar.Minimum = 0;
progressBar.Value = 0;
//每次上传4k
int bufferLength = 4096;
byte[] buffer = new byte[bufferLength];
//已上传的字节数
long offset = 0;
//开始上传时间
DateTime startTime = DateTime.Now;
int size = r.Read(buffer, 0, bufferLength);
Stream postStream = httpReq.GetRequestStream();
//发送请求头部消息
postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
while (size > 0)
{
postStream.Write(buffer, 0, size);
offset += size;
progressBar.Value = (int)(offset * (int.MaxValue / length));
TimeSpan span = DateTime.Now - startTime;
double second = span.TotalSeconds;
lblTime.Text = "已用时:" + second.ToString("F2") + "秒";
if (second > 0.001)
{
lblSpeed.Text = "平均速度:" + (offset / 1024 / second).ToString("0.00") + "KB/秒";
}
else
{
lblSpeed.Text = " 正在连接…";
}
lblState.Text = "已上传:" + (offset * 100.0 / length).ToString("F2") + "%";
lblSize.Text = (offset / 1048576.0).ToString("F2") + "M/" + (fileLength / 1048576.0).ToString("F2") + "M";
Application.DoEvents();
size = r.Read(buffer, 0, bufferLength);
}
//添加尾部的时间戳
postStream.Write(boundaryBytes, 0, boundaryBytes.Length);
postStream.Close();
//获取服务器端的响应
var webRespon = (HttpWebResponse)httpReq.GetResponse();
Stream s = webRespon.GetResponseStream();
StreamReader sr = new StreamReader(s);
//读取服务器端返回的消息
String sReturnString = sr.ReadLine();
s.Close();
sr.Close();
if (webRespon.StatusCode.ToString() == "OK")
{
returnValue = sReturnString;
progressBar1.Value = int.MaxValue;
lblState.Text = "已上传:100%";
}
else
{
returnValue = "";
}
}
catch (Exception e)
{
returnValue = "";
}
finally
{
fs.Close();
r.Close();
}
return returnValue;
}
private void UpLoad_FormClosing(object sender, FormClosingEventArgs e)
{
t.Abort();
this.Dispose();
this.Close();
}
}
}