要实现ASP.NET AJAX的无刷新上传,可以按照以下步骤操作:
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
protected void Button1_Click(object sender, EventArgs e)
{
// 获取上传的文件
HttpPostedFileBase file = FileUpload1.File;
// 获取服务器的文件路径
string filePath = Server.MapPath("~/uploads/" + file.FileName);
// 将文件保存到服务器上
file.SaveAs(filePath);
// 返回成功消息
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('File uploaded successfully!');", true);
}
<configuration>
<system.web>
<httpRuntime targetFramework="4.5" />
</system.web>
<system.webServer>
<httpHandlers>
<add name="FileUploadHandler" path="uploads/*" verb="*" type="System.Web.HttpPostedFileHandler" resourceType="Unspecified" requireAccess="Script" />
</httpHandlers>
</system.webServer>
</configuration>
以上步骤实现了ASP.NET AJAX的无刷新上传。当用户选择文件并点击上传按钮时,文件将被上传到服务器上的指定位置,而无需刷新整个页面。