vb.net ASP.NET AJAX 控件工具包文件上传

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21654628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:43:43  来源:igfitidea点击:

ASP.NET AJAX Control toolkit file upload

c#jqueryasp.netajaxvb.net

提问by user2645904

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

我在实现 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>

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

采纳答案by singularhum

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

您的 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

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.

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

Straight from the sample on the ajaxtoolkit site:

直接来自 ajaxtoolkit 站点上的示例:

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>

AjaxFileUpload 控件使用名为 AjaxFileUploadHandler.axd 的 HTTP 处理程序。该处理程序的类型为 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

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

回答by Amit Joki

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

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

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