AjaxUploadControl does not fire onuploadcomplete m

2019-07-19 17:21发布

I am trying to implement the AjaxUploadControl functionality on my site but it does not fire the OnUploadComplete method. Instead, it simply says file uploaded 100%, but the file is not in the specified folders. I have set breakpoints in the OnUploadComplete method and have been able to determine that this method is never being reached. It almost seems to be jumping into an infinite loop as the Cancel button displays, but none of the buttons on the screen are clickable once an attempt has been made to upload a file.

The .aspx page:

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

<!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 runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
        </ajaxToolkit:ToolkitScriptManager>
    <div>
            <ajaxToolkit:AjaxFileUpload ID="ajaxUpload1" runat="server" OnUploadComplete="ajaxUpload1_OnUploadComplete" />
    </div>
    </form>
</body>
</html>

The codebehind file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class AjaxTest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void ajaxUpload1_OnUploadComplete(object sender,   AjaxControlToolkit.AjaxFileUploadEventArgs e)
    {
        string filePath = "~/SiteImages/" + e.FileName;
        ajaxUpload1.SaveAs(MapPath(filePath));
    }
}

Help is greatly appreciated!

5条回答
神经病院院长
2楼-- · 2019-07-19 17:28

I discovered the problem that I am having. I was trying to implement the functionality in a website versus a web app. I created a test web app platform and it worked flawlessly. I am a beginner to AJAX and did not realize that this would cause issues, however, does anyone know a solution to get this functionality to work in a website. I'm afraid that there is too much to easily port over to web app at this time.

查看更多
3楼-- · 2019-07-19 17:31

If you don't set the right enctype and method UploadedComplete will never fire. try adding enctype="multipart/form-data" into your form tag. enter image description here e.g.

<form id="form1" runat="server" enctype="multipart/form-data" method="post">

Code behind:

protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
    string filePath = "~/SiteImages/";
    AjaxFileUpload1.SaveAs(MapPath(filePath + System.IO.Path.GetFileName(e.FileName)));
}

Code inline:

<form id="form1" runat="server" enctype="multipart/form-data" method="post">
      <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>
        <asp:AjaxFileUpload ID="AjaxFileUpload1" runat="server" 
            onuploadcomplete="AjaxFileUpload1_UploadComplete" />   
    </div>
    </form>

Hope it helps.

查看更多
劳资没心,怎么记你
4楼-- · 2019-07-19 17:34

The actual problem you were having is that you needed a HTTP handler in your web.config

<httpHandlers>
    <add verb="*" path="AjaxFileUploadHandler.axd"
      type="AjaxControlToolkit.AjaxFileUploadHandler, 
      AjaxControlToolkit"/>
</httpHandlers>

I had the very same issue (red error on upload, event in .cs file not firing at all ), now resolved by adding that to my web.config

查看更多
仙女界的扛把子
5楼-- · 2019-07-19 17:37

The property is OnClientUploadComplete, not OnUploadComplete.

Refer to this.

Events

UploadedComplete - Raised on the server when a file is uploaded successfully. In this event an instance of AjaxFileUploadEventArgs is passed in the argument that contains file name, size and content type.

查看更多
该账号已被封号
6楼-- · 2019-07-19 17:44

KINDLY FOLLOW THE FOLLOWING RULES (.NET 4.0) OR (.NET 4.5 FRAMEWORKS) MAKE SURE FOLLOWING STEPS WERE USED IN YOUR PROJECT

  1. HTML PAGE (MUST BE LIKE THIS)

    <body>
    <form id="form1" runat="server" enctype="multipart/form-data" method="post">
    <div>
        <ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></ajaxToolkit:ToolkitScriptManager>
        <ajaxToolkit:AjaxFileUpload ID="AjaxFileUpload1" runat="server" OnUploadComplete="AjaxFileUpload1_UploadComplete" ClientIDMode="Static" ContextKeys="dd" MaximumNumberOfFiles="4" />
    </div>
    </form>
    

  2. IMPORTENT WEB.Config SHOULD BE (HAVING FOLLOWING TAGS for .net 4.5)

    <system.webServer> <handlers> <add name="aa" verb="*" path="AjaxFileUploadHandler.axd" type="AjaxControlToolkit.AjaxFileUploadHandler, AjaxControlToolkit" /> </handlers> </system.webServer>

  3. IMPORTENT WEB.Config SHOULD BE (HAVING FOLLOWING TAGS for less than 4 or 4.5)

<httpHandlers> <add verb="*" path="AjaxFileUploadHandler.axd" type="AjaxControlToolkit.AjaxFileUploadHandler, AjaxControlToolkit"/> </httpHandlers>

查看更多
登录 后发表回答