要實現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的無刷新上傳。當用戶選擇文件并點擊上傳按鈕時,文件將被上傳到服務器上的指定位置,而無需刷新整個頁面。