首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ajax控制工具包fileupload

ajax控制工具包fileupload
EN

Stack Overflow用户
提问于 2013-04-28 17:57:40
回答 1查看 6.2K关注 0票数 3

我正在尝试让fileupload控件从ajax控制工具包开始工作。

我需要在后面的代码中使用上传的文件(我使用asp.net),包括解压缩、调整大小和将一些数据放到数据库中。

我遇到的问题是,当我使用ajaxUpload1_OnUploadComplete时,我无法从同一个页面上的文本框中获取文本。

当我使用断点时,我注意到文本框的值只是"“。我找了很多东西,我真的找不到解决办法,所以我希望这里的人能帮上忙。

我已经粘贴了我的代码下面,谢谢提前!

.aspx代码:

代码语言:javascript
运行
复制
<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false"
    CodeFile="Upload.aspx.vb" Inherits="_Default" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <asp:Label ID="LblUploadError" runat="server" Text="Please login first" Visible="false"></asp:Label>
    <asp:Panel ID="PnlUpload" runat="server" Visible="false">

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

        <span>Album name:</span><br />

        <asp:TextBox ID="txtAlbumNaam" runat="server" ViewStateMode="Disabled"></asp:TextBox><br />

        <span>Private album</span> <asp:CheckBox ID="chkPrivate" runat="server" /><br />

        <span>Upload files (.zip, .jpg, .jpeg or .png)</span><br />

        <asp:AjaxFileUpload id="ajaxUpload1" OnUploadComplete="ajaxUpload1_OnUploadComplete" ThrobberID="MyThrobber" runat="server" AllowedFileTypes="jpg,jpeg,zip,png"  /><br />

       <asp:Label ID="lblError" runat="server"/><br />

    </asp:Panel>
</asp:Content>

背后的代码:

代码语言:javascript
运行
复制
Imports Ionic.Zip
Imports System.IO
Imports System.Data.OleDb


Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

            If Session("LoggedIn") = True Then
                PnlUpload.Visible = True

            Else
                LblUploadError.Visible = True
            End If

    End Sub

    Protected Sub ajaxUpload1_OnUploadComplete(ByVal sender As Object, ByVal e As AjaxControlToolkit.AjaxFileUploadEventArgs)
        'indien zip:
        Dim ziperror As Boolean = False
            If System.IO.Path.GetExtension(e.FileName) = ".zip" Then
                ajaxUpload1.SaveAs(Server.MapPath("./TempZips/" & e.FileName.ToString))

                Dim ZipToUnpack As String = Server.MapPath("./TempZips/" & e.FileName.ToString)
                Dim UnpackDirectory As String = Server.MapPath("./TempFotos")

                Using zip1 As ZipFile = ZipFile.Read(ZipToUnpack)

                    Dim file As ZipEntry


                    For Each file In zip1
                        Dim strExtensie As String = System.IO.Path.GetExtension(file.ToString)
                        If Not (strExtensie = ".jpeg" Or strExtensie = ".jpg" Or strExtensie = ".png") Then
                            ziperror = True
                            lblError.Text = "The .zip structure is incorrect, please make sure that you only have compatible pictures inside the zip file."
                        End If
                    Next

                    If ziperror = False Then
                        For Each file In zip1
                            file.Extract(UnpackDirectory, ExtractExistingFileAction.OverwriteSilently)
                        Next
                    End If

                End Using
                'indien foto:
            ElseIf System.IO.Path.GetExtension(e.FileName) = ".jpeg" Or System.IO.Path.GetExtension(e.FileName) = ".jpg" Or System.IO.Path.GetExtension(e.FileName) = ".png" Then

                ajaxUpload1.SaveAs(Server.MapPath("./TempFotos/" & e.FileName.ToString))

            Else
                'indien geen van beide
                lblError.Text = "Invalid filetype on one of the files, please use other files."

            End If

            'tempzips leegmaken
        If ziperror = False Then
            For Each foundFile As String In My.Computer.FileSystem.GetFiles(Server.MapPath("TempZips"))
                File.Delete(foundFile)
            Next

            'verkleinen en album toevoegen aan database:
            Dim strFolderDirectory As String = Server.MapPath("users/" & Session("UserNickName") & "/" & txtAlbumNaam.Text)
            System.IO.Directory.CreateDirectory(strFolderDirectory)

            Dim strDirectory As String = Server.MapPath("TempFotos")
            Dim intAantalFotos As Integer = 0

            For Each foundFile As String In My.Computer.FileSystem.GetFiles(strDirectory)

                Using Afbeelding As System.Drawing.Image = System.Drawing.Image.FromFile(foundFile)

                    Dim resizedimage As System.Drawing.Image
                    Dim resizedwidth As Integer

                    resizedwidth = (300 / Afbeelding.Height) * Afbeelding.Width

                    resizedimage = Afbeelding.GetThumbnailImage(resizedwidth, 300, Nothing, New IntPtr)

                    resizedimage.Save(strFolderDirectory & "/" & Path.GetFileName(foundFile))

                End Using

                intAantalFotos += 1

            Next

            Dim CmdInsert As New OleDbCommand
            Dim Sqlstatement As String = "INSERT INTO tblAlbums (userID, createdDate, pictures, private, albumName) VALUES (@userID, Now(), @pictures, @private, @albumName);"
            CmdInsert.Connection = dbConn.cn
            CmdInsert.CommandText = Sqlstatement

            CmdInsert.Parameters.AddWithValue("userID", CInt(Session("userID")))
            CmdInsert.Parameters.AddWithValue("pictures", intAantalFotos)
            CmdInsert.Parameters.AddWithValue("private", chkPrivate.Checked)
            CmdInsert.Parameters.AddWithValue("albumName", txtAlbumNaam.Text)

            dbConn.cn.Close()
            dbConn.cn.Open()
            CmdInsert.ExecuteNonQuery()
            dbConn.cn.Close()

            'TempFotos leegmaken

            For Each foundFile As String In My.Computer.FileSystem.GetFiles(strDirectory)
                File.Delete(foundFile)
            Next

            'pagina herladen

            LblUploadError.Visible = True
            LblUploadError.Text = "Your pictures have been successfully uploaded!"
        End If

    End Sub

End Class
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-28 21:54:17

问题是ajax控件工具包文件上传控件可以使用隐藏的iFrame将文件上传到服务器(取决于浏览器支持什么HTML5功能)。隐藏的iframe引用与页面相同的url,这就是为什么您的页面再次加载,但只加载到隐藏的iFrame。这就是为什么在服务器端处理UploadComplete事件时,文本框中的值为空(因为这是加载在iframe中的页面的状态)。

问题的解决方案之一是在上传完成后执行附加逻辑(这取决于输入的数据)。要做到这一点,您可以手动处理客户端上传完整事件并执行回发(或ajax请求)。

其他解决方案可以是在开始上传之前手动设置隐藏的iFrame元素的内容。在这种情况下,您可以获得隐藏的iframe,找到必要的HTML元素(比如您的示例中的文本输入),并将其设置为与用户输入的值相同的值。但是,这种方法可能需要在客户端扩展ajax控件工具包上传控件的逻辑。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16265922

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档