C# ajax 大文件上传组件可以支持超大文件,最大支持10G,已经加入断点续传功能;本人测试效果良好,上传效率还可以。
全部代码暂不提供,只提供代码参考。
demo: http://dwz.cn/topwang
请看核心代码:
public
partial
class
MainPage : UserControl
{
private
UploadDataContext model;
private
FileUploadContext fiUpService;
private
int
UploadBlockSize = 400 * 1024;
private
bool
IsUploadPaused =
false
;
private
string
clientIP =
""
;
private
string
allowExt =
""
;
private
long
allowSize = 0;
private
string
taskID=
""
;
Dictionary<
string
,
bool
> taskList =
new
Dictionary<
string
,
bool
>();
public
MainPage()
{
InitializeComponent();
}
public
MainPage(
string
cip,
string
ext,
string
fSize)
:
this
()
{
clientIP = cip;
allowExt = FormatExt(ext);
allowSize =
long
.Parse(fSize);
}
private
string
FormatExt(
string
fileExt) {
string
[] arr=fileExt.Split(
'|'
);
string
extList=
""
;
for
(
int
i=0;i
extList+=
"*."
+arr[i];
if
(i!=arr.Length-1){
extList +=
";"
;
}
}
return
fileExt.Replace(
"|"
,
","
)+
"|"
+extList;
}
[ScriptableMember]
public
string
OnBeforeUnload()
{
if
(model.TotalUploaded != 0 && model.TotalUploaded < model.TotalFileSize)
{
return
"您正在上传文件,且尚未完成,您确定离开?"
;
}
else
{
return
string
.Empty;
}
}
private
void
UserControl_Loaded(
object
sender, RoutedEventArgs e)
{
model =
new
UploadDataContext();
this
.DataContext = model;
fiUpService =
new
FileUploadContext();
const
string
scriptableObjectName =
"Bridge"
;
HtmlPage.RegisterScriptableObject(scriptableObjectName,
this
);
string
pluginName = HtmlPage.Plugin.Id;
string
script =
string
.Format(
@"window.onbeforeunload = function () {{
var slApp = document.getElementById('{0}');
var result = slApp.Content.{1}.OnBeforeUnload();
if(result.length > 0)
return result;
}}"
, pluginName, scriptableObjectName);
HtmlPage.Window.Eval(script);
}
private
void
btnSelectFile_Click(
object
sender, RoutedEventArgs e)
{
var
dialog =
new
OpenFileDialog();
dialog.Multiselect =
true
;
dialog.Filter = allowExt;
if
(dialog.ShowDialog() ==
true
)
{
string
notAllowedFiles =
""
;
foreach
(
var
file
in
dialog.Files)
{
string
md5 = MD5.GetMd5String(clientIP + file.Name + file.Length + DateTime.Now.ToString(
"yyyy-MM-dd"
));
if
(model.UploadFileList.FirstOrDefault(x => x.FileId == md5) ==
null
)
{
if
(!IsAllowFileSize(file.Length))
{
notAllowedFiles +=
";"
+ file.Name;
continue
;
}
UploadFileInfo ufile =
new
UploadFileInfo();
ufile.FileId = md5;
ufile.FileName = file.Name;
ufile.FileSize = file.Length;
ufile.Uploaded = 0;
ufile.FileInfo = file;
model.UploadFileList.Add(ufile);
}
}
if
(notAllowedFiles.Length > 0)
{
MessageBox.Show(
"单个文件大小不能超过:"
+ formatSize(allowSize));
}
btnStartUpload.IsEnabled = model.UploadFileList.Count > 0;
}
}
private
bool
IsAllowFileSize(
long
fSize)
{
if
(
this
.allowSize > fSize ||
this
.allowSize== fSize)
{
return
true
;
}
else
{
return
false
;
}
}
private
string
formatSize(
long
value) {
if
(value>0)
{
long
fileSize = (
long
)value;
string
result;
if
(fileSize >= 1024 * 1024 * 1024)
{
result = Math.Round((
double
)fileSize / (1024 * 1024 * 1024), 2) +
" GB"
;
}
else
if
(fileSize >= 1024 * 1024)
{
result = Math.Round((
double
)fileSize / (1024 * 1024), 2) +
" MB"
;
}
else
if
(fileSize >= 1024)
{
result = Math.Round((
double
)fileSize / 1024, 2) +
" KB"
;
}
else
{
result = fileSize +
" B"
;
}
return
result;
}
else
{
return
"0 B"
;
}
}
private
void
btnStartUpload_Click(
object
sender, RoutedEventArgs e)
{
btnSelectFile.IsEnabled =
false
;
btnDelete.IsEnabled =
false
;
btnStartUpload.IsEnabled =
false
;
btnCancelUpload.IsEnabled =
true
;
btnPauseUpload.IsEnabled =
true
;
taskID = MD5.GetMd5String(DateTime.Now.ToString());
taskList.Add(taskID,
false
);
bool
isResume =
true
;
if
(isResume ==
true
)
{
foreach
(
var
item
in
model.UploadFileList)
{
fiUpService.GetFileUploadInfo(
new
UploadFileInfoContract
{
FileId = item.FileId,
FileName = item.FileName,
FileSize = item.FileSize,
Uploaded = item.Uploaded
}, OnGetFileInfoComplete,
null
);
}
}
else
{
foreach
(
var
item
in
model.UploadFileList)
{
Uploading(item);
}
}
}
private
void
OnGetFileInfoComplete(InvokeOperation
{
UploadFileInfoContract contract = invokeOp.Value;
if
(contract !=
null
)
{
UploadFileInfo ufi = model.UploadFileList.SingleOrDefault(x => x.FileId == contract.FileId);
if
(ufi !=
null
)
{
ufi.Uploaded = contract.Uploaded;
if
(ufi.FileSize>ufi.Uploaded){
Uploading(ufi);
}
else
if
(ufi.FileSize == ufi.Uploaded){
ufi.ServerUrl = contract.ServerUrl;
Uploading(ufi);
}
}
}
}
private
void
Uploading(UploadFileInfo ufi)
{
if
(taskID!=
""
){
if
(taskList.ContainsKey(taskID))
{
if
(taskList[taskID]==
true
)
{
return
;
}
}
}
lock
(
this
)
{
if
(IsUploadPaused)
{
return
;
}
if
(ufi.Uploaded < ufi.FileSize)
{
FileStream fs = ufi.FileInfo.OpenRead();
fs.Seek(ufi.Uploaded, SeekOrigin.Begin);
int
size = UploadBlockSize;
if
(size > ufi.FileSize - ufi.Uploaded)
{
size = (
int
)(ufi.FileSize - ufi.Uploaded);
}
byte
[] bytes =
new
byte
[size];
try
{
fs.Read(bytes, 0, size);
fiUpService.Uploading(
new
UploadFileInfoContract
{
FileId = ufi.FileId,
FileName = ufi.FileName,
FileSize = ufi.FileSize,
Uploaded = ufi.Uploaded
}, bytes, UploadingComplete,
null
);
}
finally
{
fs.Close();
fs.Dispose();
fs =
null
;
}
}
else
{
if
(model.TotalUploaded >= model.TotalFileSize)
{
taskList[taskID] =
true
;
UIHelper.SetTimeout(1000, () =>
{
string
files_list =
"["
;
int
i=0;
foreach
(
var
item
in
model.UploadFileList)
{
i++;
files_list +=
"{\"file_name\":\""
+item.FileName+
"\",\"file_size\":\""
+item.FileSize+
"\",\"file_url\":\""
+item.ServerUrl+
"\"}"
;
if
(i!=model.UploadFileList.Count){
files_list +=
","
;
}
}
files_list +=
"]"
;
model.UploadFileList.Clear();
btnSelectFile.IsEnabled =
true
;
btnDelete.IsEnabled =
true
;
btnStartUpload.IsEnabled =
false
;
btnCancelUpload.IsEnabled =
false
;
IsUploadPaused =
false
;
btnPauseUpload.Content =
"暂停"
;
btnPauseUpload.IsEnabled =
false
;
HtmlPage.Window.Invoke(
"FileUploadCompleteJs"
, files_list);
});
}
}
}
}
private
void
UploadingComplete(InvokeOperation
{
UploadFileInfoContract contract = invokeOp.Value;
if
(contract !=
null
)
{
UploadFileInfo ufi = model.UploadFileList.SingleOrDefault(x => x.FileId == contract.FileId);
if
(ufi !=
null
)
{
ufi.Uploaded = contract.Uploaded;
ufi.ServerUrl = contract.ServerUrl;
Uploading(ufi);
}
}
}
private
void
btnCancelUpload_Click(
object
sender, RoutedEventArgs e)
{
lock
(
this
)
{
IsUploadPaused =
true
;
btnPauseUpload.IsEnabled =
false
;
ConfirmWin win =
new
ConfirmWin();
win.Message =
"取消上传将会清空当前已上传部分文件,\r\n您确定要取消上传?"
;
win.Closed +=
new
EventHandler(CancelConfirmWin_Closed);
win.Show();
}
}
void
CancelConfirmWin_Closed(
object
sender, EventArgs e)
{
ConfirmWin win = sender
as
ConfirmWin;
if
(win.DialogResult ==
true
)
{
List
new
List
foreach
(
var
item
in
model.UploadFileList)
{
UploadFileInfoContract c =
new
UploadFileInfoContract();
c.FileId = item.FileId;
c.FileName = item.FileName;
c.FileSize = item.FileSize;
c.Uploaded = item.Uploaded;
list.Add(c);
}
fiUpService.CancelUpload(list, CancelUploadComplete,
null
);
taskList.Remove(taskID);
taskID =
""
;
}
else
{
IsUploadPaused =
false
;
btnPauseUpload.Content =
"暂停"
;
btnPauseUpload.IsEnabled =
true
;
}
}
private
void
CancelUploadComplete(InvokeOperation
{
List
if
(list !=
null
)
{
foreach
(
var
item
in
list)
{
UploadFileInfo ufi = model.UploadFileList.SingleOrDefault(x => x.FileId == item.FileId);
if
(ufi !=
null
)
{
ufi.Uploaded = item.Uploaded;
}
}
}
IsUploadPaused =
false
;
btnSelectFile.IsEnabled =
true
;
btnDelete.IsEnabled =
true
;
btnStartUpload.IsEnabled =
true
;
btnPauseUpload.IsEnabled =
false
;
btnPauseUpload.Content =
"暂停"
;
btnCancelUpload.IsEnabled =
false
;
}
private
void
btnDelete_Click(
object
sender, RoutedEventArgs e)
{
if
(grdFileList.SelectedItems !=
null
)
{
List
new
List
foreach
(
var
item
in
grdFileList.SelectedItems)
{
UploadFileInfo fi = item
as
UploadFileInfo;
if
(fi !=
null
)
{
selectedItems.Add(fi);
}
}
int
i = 0;
foreach
(
var
item
in
selectedItems)
{
i++;
model.UploadFileList.Remove(item);
}
if
(i == 0)
{
MessageBox.Show(
"您没有选中任何文件,请先选中文件。"
);
}
if
(model.UploadFileList.Count == 0)
{
btnStartUpload.IsEnabled =
false
;
}
}
}
private
void
btnPauseUpload_Click(
object
sender, RoutedEventArgs e)
{
lock
(
this
)
{
IsUploadPaused = !IsUploadPaused;
if
(IsUploadPaused)
{
btnPauseUpload.Content =
"继续"
;
}
else
{
btnPauseUpload.Content =
"暂停"
;
if
(taskList[taskID] ==
false
)
{
foreach
(
var
item
in
model.UploadFileList)
{
fiUpService.GetFileUploadInfo(
new
UploadFileInfoContract
{
FileId = item.FileId,
FileName = item.FileName,
FileSize = item.FileSize,
Uploaded = item.Uploaded
}, OnGetFileInfoComplete,
null
);
}
}
}
}
}
}
|