3

我正在尝试fileuploadajax 控件工具包中获取控件来工作。

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

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

当我使用断点时,我注意到文本框的值只是“”。我已经搜索了很多,我真的找不到解决方案,所以我希望这里有人可以提供帮助。

我已经在下面粘贴了我的代码,在此先感谢!

.aspx 代码:

<%@ 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>

后面的代码:

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
4

1 回答 1

3

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

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

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

于 2013-04-28T21:54:17.653 回答