ASP.NET AJAX Control工具包文件上传

时间:2014-02-09 02:51:18

标签: c# jquery asp.net ajax vb.net

我在实现ajax控件工具包文件上传时遇到一些困难。这就是我要提出的,以下是我的代码。我很抱歉使用VB,我被迫。如果您知道C#解决方案,请提供它,我将转换代码。谢谢

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>

背后的代码

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

2 个答案:

答案 0 :(得分:2)

永远不会调用您的UploadComplete方法,因为它永远不会被处理。像这样添加控件的UploadComplete事件:

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

由于文件上传控件此时仅将文件保存到临时位置,因为您的处理程序从未被调用,我相信您获得的错误与未设置 HTTP处理程序有关。否则,请指明错误。

直接从ajaxtoolkit网站上的样本:

  

AjaxFileUpload控件使用名为的HTTP处理程序   AjaxFileUploadHandler.axd此处理程序具有类型   AjaxControlToolkit.AjaxFileUploadHandler。您必须将此处理程序添加到   您的Web.Config文件,以便AjaxFileUpload控件工作。

     

这是您必须添加的Web.Config配置:

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

对于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

答案 1 :(得分:1)

您必须在web.config中添加以下内容才能使其正常工作。

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