Question:
Fix Asp.net issue: AjaxControlToolkit FileUpload not working

Problem:

in my aspx:

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>

    <ajaxToolkit:AjaxFileUpload ID="ajaxFileUpload" runat="server" OnUploadComplete="UploadComplete"     AllowedFileTypes="xlsx" MaximumNumberOfFiles="1" ClientIDMode="Static" />


in my aspx.cs:

protected void UploadComplete(object sender, AjaxFileUploadEventArgs e)

    {

    // Handle the uploaded file here

    string filePath = Server.MapPath("~/Templates/Upload/" + e.FileName);

    ajaxFileUpload.SaveAs(filePath);

    }

I am having an issue that on upload the file, it does not break into the UploadComplete function in my aspx.cs


Solution:

Ok, so it not clear how you installed the toolkit (NuGet, or a zip download).

A simple drag + drop of the ajaxfileupload control should wire up the page correctly.

However, you also need a script manager on the page (and you need that script manager for just about any control from the toolkit).


So, the process with an empty page looks like this:


As you can see, the above automatic adds the assembly reference to the page.

And as noted, the page did have a script manager dropped into the page (drop in the script manager right after the form tag).


So, our page markup now looks like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UpLoadTest.aspx.cs" Inherits="Web35.UpLoadTest" %>


<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>



<!DOCTYPE html>


<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

        <div style="padding:35px;width:40%">



            <ajaxToolkit:AjaxFileUpload ID="AjaxFileUpload1" runat="server"

            

                />


        </div>


    </form>

</body>

</html>


At this point, we could (should) try using the property sheet to add the upload file event.

So, select the control, and now display the property sheet.

So, then this:


So, in most cases, let vs write and create the event stub for you.

The above created this event stub, and let's add some code to save the file.


  protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)

    {

        // One file uploaded, save it


        string sFileAndPath = Server.MapPath($@"~\UpLoadFiles\{e.FileName}");


        AjaxFileUpload1.SaveAs(sFileAndPath);


    }


However, before ANY of the above?


We have to add to the web.config in the config section, add this:

<system.webServer>


<handlers>

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

</handlers>

<validation validateIntegratedModeConfiguration="false" />

<security>

  <requestFiltering>

    <requestLimits maxAllowedContentLength="2147483648" />

  </requestFiltering>

</security>

</system.webServer>


(you probably have a handler’s section in web.config, so use that).


As you can see, this is why I again suggest you use NuGet to install the toolkit.


The above should result in the control now working for you.


The end result is a really nice upload control. It allows un-limited size and has a drag + drop hot spot. So, say this:


Suggested blogs:

>Create a test case for add user functionality in a movie app - Python

>Avoid code duplication between django Q expression and Python

>How can I iterate over enum Flag alias?

>How to train a deep learning in case validation with Pytorch result NaN or high loss?s

>Fixing QVariant output issue in Python

>Removing ASCII formatted characters from my CAN messages in Python


Ritu Singh

Ritu Singh

Submit
0 Answers