AsyncFileUpload控件,就是用来对文件进行异步上传的,示例看这里:http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/AsyncFileUpload/AsyncFileUpload.aspx。有需要异步上传文件的同学要看这里了,具体用法如下:
前台代码:
<ajaxToolkit:AsyncFileUpload OnClientUploadError="uploadError" OnClientUploadComplete="uploadComplete"
runat="server" ID="AsyncFileUpload1" Width="400px" UploaderStyle="Modern"
UploadingBackColor="#CCFFFF"
onuploadedcomplete="AsyncFileUpload1_UploadedComplete" />
runat="server" ID="AsyncFileUpload1" Width="400px" UploaderStyle="Modern"
UploadingBackColor="#CCFFFF"
onuploadedcomplete="AsyncFileUpload1_UploadedComplete" />
说说属性吧,
OnClientUploadError:客户端错误处理方法
OnClientUploadComplete:客户端上传完成事件
UploaderStyle:控件的风格,有传统和现代两种
OnUploadedComplete:服务器端上传完成处理事件,在这个事件中可以调用SaveAs方法,指定文件的存储位置和名称等。
前台js代码:
function uploadComplete(sender, args) {
alert("上传成功");
}
function uploadError(sender, args) {
alert("上传错误");
}
alert("上传成功");
}
function uploadError(sender, args) {
alert("上传错误");
}
后台代码:
protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
AjaxControlToolkit.AsyncFileUpload fileUploader = sender as AjaxControlToolkit.AsyncFileUpload;
if (fileUploader != null && fileUploader.HasFile)
{
fileUploader.SaveAs(Server.MapPath("/Resource/UploadFile/" + fileUploader.FileName));
}
}
{
AjaxControlToolkit.AsyncFileUpload fileUploader = sender as AjaxControlToolkit.AsyncFileUpload;
if (fileUploader != null && fileUploader.HasFile)
{
fileUploader.SaveAs(Server.MapPath("/Resource/UploadFile/" + fileUploader.FileName));
}
}