1
using System;
2
using System.Collections;
3
using System.IO;
4
using System.Text;
5
using System.Web;
6
7
namespace Webb.WAVE.Controls.Upload
8
...{
9
//该源码下载自www.51aspx.com(51aspx.com)
10
11
/**//// <summary>
12
///
13
/// </summary>
14
internal class WebbRequestStream : IDisposable
15
...{
16
Enums#region Enums
17
public enum FileStatus : byte...{Close = 1, Open = 0}
18
#endregion
19
20
Fields#region Fields
21
private long i_boundarySize;
22
private byte[] m_boundaryFlag;
23
private byte[] m_flag;
24
private FileStream m_fs;
25
private ArrayList m_contentTextBody;
26
private ArrayList m_tempBoundary;
27
private ArrayList m_tempContent;
28
private ArrayList m_templist1;
29
private ArrayList m_templist2;
30
private FileStatus m_fileStatus;
31
private int m_boundaryIndex;
32
private int m_contentIndex;
33
private string m_tempPath;
34
private HttpContext m_content;
35
public string m_rawFileName;
36
#endregion
37
38
Propetries#region Propetries
39
public ArrayList ContentTextBody
40
...{
41
get ...{ return this.m_contentTextBody; }
42
}
43
#endregion
44
45
public WebbRequestStream()
46
...{
47
this.m_fileStatus = FileStatus.Close;
48
this.m_contentTextBody = new ArrayList();
49
this.m_tempContent = new ArrayList();
50
this.m_tempBoundary = new ArrayList();
51
this.m_boundaryIndex = 2;
52
this.m_contentIndex = 0;
53
this.m_content = null;
54
this.m_flag = Encoding.ASCII.GetBytes("\r\n\r\n");
55
m_templist1 = new ArrayList();
56
m_templist2 = new ArrayList();
57
}
58
public void SetBoundaryFlag(byte[] i_boundaryFlag)
59
...{
60
m_boundaryFlag = new byte[i_boundaryFlag.Length];
61
i_boundaryFlag.CopyTo(m_boundaryFlag,0);
62
i_boundarySize = m_boundaryFlag.Length;
63
}
64
public void SetHttpContent(HttpContext i_content)
65
...{
66
this.m_content = i_content;
67
}
68
public void SetTempPath(string i_tempPath)
69
...{
70
this.m_tempPath = i_tempPath;
71
}
72
73
unsafe public void TransactReadData(byte[] m_readData)
74
...{
75
/**////There are 5 case when the data come in.
76
///1. Content data, write into content.
77
///2. Upload File data, write into file.
78
///3. Break point at boundary, continue to find boundary.
79
///4. Break point at contentHeader, continue to find contentHeader
80
...#region
81
if(m_readData.Length==0||this.m_content==null||this.m_boundaryFlag.Length==0)
82
...{
83
return;
84
}
85
long i_currentIndex = 0;
86
long i_totalSize = m_readData.Length;
87
bool m_firstCallFunction = true;
88
#endregion
89
while(i_currentIndex<i_totalSize)
90
...{
91
if(m_readData[i_currentIndex]==13||m_firstCallFunction)
92
...{
93
m_firstCallFunction = false;
94
while(m_boundaryIndex<i_boundarySize&&i_currentIndex<i_totalSize)
95
...{
96
search for boundary flag#region search for boundary flag
97
if(m_readData[i_currentIndex]!=this.m_boundaryFlag[m_boundaryIndex])
98
...{
99
break;
100
}
101
this.m_templist2.Add(m_readData[i_currentIndex]);
102
m_boundaryIndex++;
103
i_currentIndex++;
104
#endregion
105
}
106
if(m_boundaryIndex==i_boundarySize)
107
...{
108
Find a boundary and not the data end.#region Find a boundary and not the data end.
109
if(i_currentIndex+2<i_totalSize)
110
...{
111
this.m_templist2.Add(m_readData[i_currentIndex]);
112
i_currentIndex++;
113
this.m_templist2.Add(m_readData[i_currentIndex]);
114
i_currentIndex++;
115
}
116
this.m_contentTextBody.AddRange(m_templist2);
117
this.m_templist2.Clear();
118
if(this.m_fileStatus==FileStatus.Open&&this.m_fs!=null)
119
...{
120
Close the current open file.#region Close the current open file.
121
m_fs.Flush();
122
m_fs.Close();
123
m_fs = null;
124
this.m_fileStatus = FileStatus.Close;
125
#endregion
126
}
127
/**////Search for content header.
128
while(i_currentIndex<i_totalSize)
129
...{
130
...#region
131
while(i_currentIndex<i_totalSize&&m_contentIndex<4)
132
...{
133
search for content header end.#region search for content header end.
134
if(m_flag[m_contentIndex]!=m_readData[i_currentIndex])
135
...{
136
break;
137
}
138
m_templist1.Add(m_readData[i_currentIndex]);
139
m_contentIndex++;
140
i_currentIndex++;
141
#endregion
142
}
143
if(m_contentIndex==4)
144
...{
145
//Find a contend header#region //Find a contend header
146
this.m_tempContent.AddRange(m_flag);
147
m_templist1.Clear();
148
byte[] m_temp1 = new byte[m_tempContent.Count];
149
m_tempContent.CopyTo(m_temp1);
150
string m_contentData = HttpContext.Current.Request.ContentEncoding.GetString(m_temp1);
151
if(m_contentData.IndexOf("\"; filename=\"")<0)
152
...{
153
//This is other data.
154
this.m_contentTextBody.AddRange(m_tempContent);
155
this.m_fileStatus = FileStatus.Close;
156
}
157
else
158
...{
159
//This is a upload file data.
160
string[] m_fileContent = this.GetFileContent(m_contentData);
161
string m_filePath = this.GreateFileStream(m_fileContent[2]);
162
StringBuilder sb = new StringBuilder();
163
string[] sbArray = new string[11];
164
sbArray[0] = m_fileContent[0];
165
sbArray[1] = ";";
166
sbArray[2] = m_fileContent[1];
167
sbArray[3] = "\r\n\r\n";
168
sbArray[4] = m_fileContent[3];
169
sbArray[5] = ";";
170
sbArray[6] = m_fileContent[2];
171
sbArray[7] = "; ";
172
sbArray[8] = "filePath=\"";
173
sbArray[9] = m_filePath;
174
sbArray[10] = "\"";
175
sb.Append(string.Concat(sbArray));
176
this.m_contentTextBody.AddRange(Encoding.UTF8.GetBytes(sb.ToString().ToCharArray()));
177
sb.Remove(0, sb.Length);
178
this.m_fileStatus = FileStatus.Open;
179
}
180
m_tempContent.Clear();
181
m_templist1.Clear();
182
m_contentIndex = 0;
183
m_boundaryIndex = 0;
184
#endregion
185
break;
186
}
187
else if(i_currentIndex==i_totalSize)
188
...{
189
//The break point is in the content header, just break, the next will be go on.
190
break;
191
}
192
else if(m_templist1.Count>0)
193
...{
194
m_tempContent.AddRange(m_templist1);
195
m_templist1.Clear();
196
m_contentIndex = 0;
197
}
198
m_tempContent.Add(m_readData[i_currentIndex]);
199
#endregion
200
i_currentIndex++;
201
}
202
#endregion
203
}
204
}
205
if(i_currentIndex>=i_totalSize) break;
206
if(m_readData[i_currentIndex]==13)
207
...{
208
If the file data contaion \r, then we should check it.#region If the file data contaion \r, then we should check it.
209
long m_temp = i_currentIndex;
210
int m_temp2 = 0;
211
while(m_temp2<i_boundarySize&&i_currentIndex<i_totalSize)
212
...{
213
if(m_readData[i_currentIndex]!=m_boundaryFlag[m_temp2])
214
...{
215
break;
216
}
217
m_temp2++;
218
i_currentIndex++;
219
}
220
//However, the if the file is open, the data should writ into file.
221
if(m_fileStatus==FileStatus.Open&&m_templist2.Count>0)
222
...{
223
byte[] temp = new byte[m_templist2.Count];
224
m_templist2.CopyTo(temp);
225
m_fs.Write(temp ,0,m_templist2.Count);
226
m_templist2.Clear();
227
temp = null;
228
}
229
if(m_temp2==i_boundarySize)
230
...{
231
//If here is a boundary, should check the data again, so write down the "i_currentIndex", and check again.
232
m_boundaryIndex = 0;
233
i_currentIndex = m_temp;
234
continue;
235
}
236
else
237
...{
238
//Or recheck the data
239
i_currentIndex = m_temp;
240
}
241
#endregion
242
}
243
if(m_fileStatus==FileStatus.Open)
244
...{
245
//wirte to file#region //wirte to file
246
if(m_templist2.Count>0)
247
...{
248
byte[] temp = new byte[m_templist2.Count];
249
m_templist2.CopyTo(temp);
250
m_fs.Write(temp,0,temp.Length);
251
temp = null;
252
}
253
m_fs.WriteByte(m_readData[i_currentIndex]);
254
#endregion
255
}
256
else
257
...{
258
write to context#region write to context
259
if(m_tempBoundary.Count>0)
260