温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:51aspx大文件上传并显示进度和上传速率示例及组建源码
当前文件:
LargeFileUpload/WebbUpload/UploadFile.cs,打开代码结构图
LargeFileUpload/WebbUpload/UploadFile.cs,打开代码结构图1using System; 2
using System.IO; 3
4
namespace Webb.WAVE.Controls.Upload 5
{ 6
/// <summary> 7
/// 8
/// </summary> 9
public class UploadFile 10
{ 11
Properties 55
56
Fields 63
/// <summary> 64
/// 65
/// </summary> 66
/// <param name="name"></param> 67
public UploadFile(string i_name) 68
{ 69
if(i_name == null||i_name == string.Empty) 70
{ 71
return; 72
// throw new ArgumentNullException("i_name", "Name can not be null!"); 73
} 74
75
string m_content = string.Empty; 76
this.m_clientName = string.Empty; 77
this.m_filename = string.Empty; 78
this.m_contenttype = string.Empty; 79
this.m_filelength = 0; 80
81
if(IsContentHeader(i_name)) 82
{ 83
m_content = i_name; 84
} 85
else if(IsContentHeader(WebbHelper.GetContext().Request[i_name])) 86
{ 87
m_content = WebbHelper.GetContext().Request[i_name]; 88
} 89
if((m_content==null)||(m_content==string.Empty)) 90
{ 91
return; 92
} 93
//Get file info from content. 94
string[] contentArray = m_content.Split(';'); 95
string m_temp = contentArray[0]; 96
this.m_contenttype = m_temp.Substring(m_temp.IndexOf(":")+1).Trim(); 97
m_temp = contentArray[1]; 98
this.m_clientName = m_temp.Substring(m_temp.IndexOf("\"")+1,m_temp.LastIndexOf("\"")-m_temp.IndexOf("\"")-1).Trim(); 99
m_temp = contentArray[2]; 100
this.m_filename = m_temp.Substring(m_temp.IndexOf("\"")+1,m_temp.LastIndexOf("\"")-m_temp.IndexOf("\"")-1).Trim(); 101
string uploadFolder = WebbHelper.GetUploadFolder(); 102
//string uploadFolder = @"C:\Inetpub\wwwroot\WebbTest\Upload"; 103
if(this.m_filename==null||this.m_filename==string.Empty) return; 104
this.m_filePath = Path.Combine(uploadFolder, this.m_filename); 105
try 106
{ 107
this.m_filelength = new FileInfo(this.m_filePath).Length; 108
} 109
catch (Exception exception) 110
{ 111
string uploadGuid = WebbHelper.GetContext().Request["Webb_Upload_GUID"]; 112
if (uploadGuid != null) 113
{ 114
WebbHelper.GetContext().Application.Remove(("Upload_Status_" + uploadGuid)); 115
} 116
throw exception; 117
} 118
} 119
120
/// <summary> 121
/// 122
/// </summary> 123
/// <param name="line"></param> 124
/// <returns></returns> 125
private bool IsContentHeader(string i_line) 126
{ 127
//WebbSystem.TraceMsg(line); 128
if((i_line == null)||(i_line == String.Empty)) 129
{ 130
return false; 131
} 132
string[] contentArray = i_line.Split(';'); 133
134
if (contentArray.Length==3 135
&& contentArray[0].IndexOf("Content-Type:")>=0 136
&& contentArray[1].IndexOf("filename=\"")>=0 137
&& contentArray[2].IndexOf("filePath=\"")>=0) 138
{ 139
return true; 140
} 141
return false; 142
} 143
144
/// <summary> 145
/// Save file to disk. 146
/// </summary> 147
/// <param name="filename"></param> 148
public void SaveAs(string filename) 149
{ 150
string uploadGuid = WebbHelper.GetContext().Request["Webb_Upload_GUID"]; 151
string m_fileName = Path.GetFileName(filename); 152
if(m_fileName==null||m_fileName==string.Empty) return; 153
if(this.m_filePath==null||this.m_filePath==string.Empty) return; 154
try 155
{ 156
UploadStatus uploadStatus; 157
FileInfo fileInfo = new FileInfo(this.m_filePath); 158
if (uploadGuid != null) 159
{ 160
uploadStatus = new UploadStatus(); 161
uploadStatus.GetUploadStatus(uploadGuid); 162
uploadStatus.Status = UploadStatus.UploadState.Moving; 163
WebbHelper.GetContext().Application[("Upload_Status_" + uploadGuid)] = uploadStatus; 164
} 165
166
string directoryName = Path.GetDirectoryName(filename); 167
if (!Directory.Exists(directoryName)) 168
{ 169
Directory.CreateDirectory(directoryName); 170
} 171
else if (File.Exists(filename)) 172
{ 173
File.Delete(filename); 174
} 175
//Move temporary file to file that client uploaded. Simply change it's name. 176
fileInfo.MoveTo(filename); 177
if (uploadGuid == null) 178
{ 179
return; 180
} 181
uploadStatus = new UploadStatus(); 182
uploadStatus.GetUploadStatus(uploadGuid); 183
uploadStatus.Status = UploadStatus.UploadState.Completed; 184
WebbHelper.GetContext().Application[("Upload_Status_"+uploadGuid)]=uploadStatus; 185
} 186
catch (Exception exception) 187
{ 188
if (uploadGuid != null) 189
{ 190
WebbHelper.GetContext().Application.Remove(("Upload_Status_" + uploadGuid)); 191
} 192
throw exception; 193
} 194
} 195
} 196
}





}