user2645904
user2645904

Reputation: 169

ASP.NET AJAX Control toolkit file upload

I'm having some difficulty implementing the ajax control toolkit file upload. This is what i have coming up and the following is my code. I'm sorry for using VB, i was forced. If you know a C# solution please provide it and i will convert the code. Thank you

a busy cat

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm2.aspx.vb" Inherits="AMS.WebForm2" %>

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>

<form id="form1" runat="server">
    <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" />

    <asp:AjaxFileUpload ID="AjaxFileUpload1" runat="server" />
    <asp:Image ID="loader" runat="server"
        ImageUrl="~/loading.gif" Style="display: None" />
</form>
</body>
</html>

Code behind

Imports System.Web.Script.Serialization
Imports AjaxControlToolkit

Public Class WebForm2
Inherits System.Web.UI.Page

Protected Sub UploadComplete(sender As Object, e As AjaxControlToolkit.AjaxFileUploadEventArgs)
    Dim path As String = Server.MapPath("Files/") + e.FileName
    AjaxFileUpload1.SaveAs(path)
End Sub
End Class

Upvotes: 2

Views: 15614

Answers (2)

Amit Joki
Amit Joki

Reputation: 59252

You must add the following in web.config to make it work.

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

Upvotes: 1

singularhum
singularhum

Reputation: 5122

Your UploadComplete method will never be called because it is never handled. Add the UploadComplete event of the control like so:

Protected Sub UploadComplete(sender As Object, e As AjaxControlToolkit.AjaxFileUploadEventArgs) Handles AjaxFileUpload1.UploadComplete
    Dim path As String = Server.MapPath("Files/") + e.FileName
    AjaxFileUpload1.SaveAs(path)
End Sub

Since the file upload control is only saving the file to a temp location at this point since your handler is never called, I believe the error you're getting is related to not setting an HTTP handler. Otherwise, please specify the error.

Straight from the sample on the ajaxtoolkit site:

The AjaxFileUpload control uses an HTTP Handler named AjaxFileUploadHandler.axd This handler has the type AjaxControlToolkit.AjaxFileUploadHandler. You must add this handler to your Web.Config file in order for the AjaxFileUpload control to work.

Here's the Web.Config configuration that you must add:

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

For IIS7:

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

http://www.asp.net/AjaxLibrary/AjaxControlToolkitSampleSite/AjaxFileUpload/AjaxFileUpload.aspx

Upvotes: 2

Related Questions