asp.net 用ajax实现文件上传

前台代码

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Anthem.NET FileUpload Demo</title>
    <style type="text/css">
        *
        {
            margin: 3px;
            font-family: tahoma;
            font-size: 11px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <fieldset>
            <legend>Default ASP.NET FileUpload</legend>
            <asp:FileUpload ID="defaultFileUpload" runat="server" />
            <asp:Button ID="defaultUploadButton" runat="server" OnClick="defaultUploadButton_Click" Text="Upload" />
            <asp:Label ID="defaultResultLabel" runat="server" Text=""></asp:Label>
        </fieldset>
           
        <fieldset>
            <legend>Anthem.NET FileUpload</legend>
            <anthem:FileUpload ID="anthemFileUpload" runat="server" />
            <anthem:Button ID="anthemUploadButton" TextDuringCallBack="正在上传中,请稍后" EnabledDuringCallBack="false"
                runat="server" Text="Upload" OnClick="anthemUploadButton_Click" />
            <anthem:Label ID="anthemResultLabel" runat="server" Text=""></anthem:Label>
        </fieldset>
    </form>
</body>
</html>

后台:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Threading.Thread.Sleep(3000);
    }
    protected void defaultUploadButton_Click(object sender, EventArgs e)
    {
        defaultResultLabel.Text = string.Format("File \"{0}\" uploaded ({1} bytes).",
            defaultFileUpload.FileName,
            defaultFileUpload.FileBytes.Length
        );
    }
    void UpLoad(string path, FileUpload fileupload)
    {
        bool isOK = false;
        if (fileupload.HasFile)
        {
            string fileExtention = Path.GetExtension(path).ToLower();
            string[] allowedException = { ".xls", ".doc", ".mpp", ".rar", ".zip", ".vsd", ".txt", ".jpg", ".gif", ".bmp", ".png", ".swf", ".avi", ".mp3", ".rm", ".wma", ".wmv" };
            foreach (string s in allowedException)
            {
                if (s == fileExtention)
                {
                    isOK = true;
                }
            }
            if (isOK)
            {
                if (Directory.Exists(path) == false)
                {
                    Directory.CreateDirectory(path);
                }
            }
            fileupload.SaveAs(Server.MapPath(path));
          
            anthemResultLabel.UpdateAfterCallBack = true;
            Page.RegisterStartupScript("info", "<script>alert('上传成功');</script>");
        }
    }
    protected void anthemUploadButton_Click(object sender, EventArgs e)
    {
        if (this.anthemFileUpload.FileName != null || this.anthemFileUpload.FileName != "")
        {
            if (this.anthemFileUpload.FileContent.Length <= 0)
            {
                Page.RegisterStartupScript("info", "<script>alert('上传文件为空文件,请重新选择!')</script>");
                return;
            }
            if (this.anthemFileUpload.HasFile)
            {
                if (this.anthemFileUpload.FileContent.Length > 96156717)
                {
                    Page.RegisterStartupScript("info", "<script>alert('上传文件太大,请重新选择!')</script>");
                }
                else
                {
                    string strUrl = "~/VideoSrc/files/" + this.anthemFileUpload.FileName;
                    UpLoad(strUrl, this.anthemFileUpload);
                }
            }
        }
        anthemResultLabel.Text = string.Format("File \"{0}\" uploaded ({1} bytes).",
            anthemFileUpload.FileName,
            anthemFileUpload.FileBytes.Length
        );
        Page.RegisterStartupScript("info", "<script>alert('上传成功');</script>");
    }
}


需要添加的组件

原文地址:https://www.cnblogs.com/wangyhua/p/4050665.html