AjaxUploadControl does not fire onuploadcomplete method

随声附和 提交于 2019-12-11 04:17:50

问题


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!


回答1:


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

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.




回答2:


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




回答3:


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.




回答4:


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.




回答5:


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>



来源:https://stackoverflow.com/questions/13457639/ajaxuploadcontrol-does-not-fire-onuploadcomplete-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!