AJAX控件工具包文件上传 [英] ajax control toolkit fileupload

查看:101
本文介绍了AJAX控件工具包文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让从的文件上传控制的http://ajaxcontroltoolkit.$c$cplex.com/相对=nofollow > AJAX控件工具包的工作。

我需要使用上传的文件在我的背后code(我用asp.net) 这包括解压缩,调整和把一些数据在数据库中。

我的问题是,当我使用 ajaxUpload1_OnUploadComplete ,我不能从一个文本框,在同一页上的文字。

当我使用断点,我注意到,该文本框的值就是。 我寻觅了很多,我实在无法找到一个解决方案,所以我希望有人在这里也许能够提供帮助。

我已经粘贴下面我code,先谢谢了!

的.aspx code:

 <%@页标题=LANGUAGE =VB的MasterPageFile =〜/ MasterPage.masterAutoEventWireup =假
    codeFILE =Upload.aspx.vb继承=_默认%>

<%@注册议会=AjaxControlToolkit中命名空间=AjaxControlToolkit中标签preFIX =ASP%>
< ASP:内容ID =内容1ContentPlaceHolderID =头=服务器>
< / ASP:内容>
< ASP:内容ID =内容2ContentPlaceHolderID =ContentPlaceHolder1=服务器>
    < ASP:标签ID =LblUploadError=服务器文本=请先登录可见=假>< / ASP:标签>
    < ASP:面板ID =PnlUpload=服务器可见=假>

    < ASP:ToolkitScriptManager ID =ToolkitScriptManager1=服务器>< / ASP:ToolkitScriptManager>

        <跨度>作品名:< / SPAN>< BR />

        < ASP:文本框ID =txtAlbumNaam=服务器ViewStateMode =已禁用>< / ASP:文本框>< BR />

        <跨度>私人相册< / SPAN> < ASP:复选框ID =chkPrivate=服务器/>< BR />

        <跨度>上传文件(.ZIP,.JPG,.JPEG或.png)LT; / SPAN>< BR />

        < ASP:AjaxFileUpload ID =ajaxUpload1OnUploadComplete =ajaxUpload1_OnUploadCompleteThrobberID =MyThrobber=服务器AllowedFileTypes =JPG,JPEG,ZIP,PNG/>< BR />

       < ASP:标签ID =lblError=服务器/>< BR />

    < / ASP:面板>
< / ASP:内容>
 

后面的 code:

 进口Ionic.Zip
进口System.IO
进口System.Data.OleDb


部分类_Default
    继承System.Web.UI.Page
    保护小组的Page_Load(发送者为对象,E作为System.EventArgs)把手Me.Load

            如果会议(的loggedIn)= true,那么
                PnlUpload.Visible = TRUE

            其他
                LblUploadError.Visible = TRUE
            结束如果

    结束小组

    保护小组ajaxUpload1_OnUploadComplete(BYVAL发件人为对象,BYVAL E上AjaxControlToolkit.AjaxFileUploadEventArgs)
        indien邮编:
        昏暗ziperror由于布尔= FALSE
            如果System.IO.Path.GetExtension(e.FileName)=.ZIP然后
                ajaxUpload1.SaveAs(使用Server.Mappath(./ TempZips /&放大器; e.FileName.ToString))

                昏暗ZipToUnpack作为字符串=使用Server.Mappath(./ TempZips /&放大器; e.FileName.ToString)
                昏暗UnpackDirectory作为字符串=使用Server.Mappath(./ TempFotos)

                使用ZIP1作为的ZipFile = ZipFile.Read(ZipToUnpack)

                    昏暗的文件作为的ZipEntry


                    对于每个文件在ZIP1
                        昏暗strExtensie作为字符串= System.IO.Path.GetExtension(file.ToString)
                        如果不是(strExtensie =.JPEG或者strExtensie =.JPG或strExtensie =巴纽),然后
                            ziperror = TRUE
                            lblError.Text =该.zip结构不正确,请确保您只有zip文件内兼容的图片。
                        结束如果
                    下一个

                    如果ziperror = false,那么
                        对于每个文件在ZIP1
                            file.Extract(UnpackDirectory,ExtractEx​​istingFileAction.OverwriteSilently)
                        下一个
                    结束如果

                结束使用
                indien照片:
            elseif的System.IO.Path.GetExtension(e.FileName)=.JPEG或者System.IO.Path.GetExtension(e.FileName)=.JPG或System.IO.Path.GetExtension(e.FileName)= .png格式那

                ajaxUpload1.SaveAs(使用Server.Mappath(./ TempFotos /&放大器; e.FileName.ToString))

            其他
                indien吉恩面包车贝德玛
                lblError.Text =无效文件类型的文件之一,请用其他文件。

            结束如果

            tempzips leegmaken
        如果ziperror = false,那么
            对于每个foundFile作为字符串在My.Computer.FileSystem.GetFiles(使用Server.Mappath(TempZips))
                File.Delete(foundFile)
            下一个

            verkleinen恩专辑toevoegen AAN数据库:
            昏暗strFolderDirectory作为字符串=使用Server.Mappath(用户/&放大器;会议(UserNickName)及/和安培; txtAlbumNaam.Text)
            System.IO.Directory.CreateDirectory(strFolderDirectory)

            昏暗strDirectory作为字符串=使用Server.Mappath(TempFotos)
            昏暗intAantalFotos为整数= 0

            对于每个foundFile作为字符串在My.Computer.FileSystem.GetFiles(strDirectory)

                使用Afbeelding作为System.Drawing.Image对象= System.Drawing.Image.FromFile(foundFile)

                    昏暗resizedimage作为System.Drawing.Image对象
                    昏暗resizedwidth作为整数

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

                    resizedimage = Afbeelding.GetThumbnailImage(resizedwidth,300,没有什么新的IntPtr)

                    resizedimage.Save(strFolderDirectory&安培;/&放大器; Path.GetFileName(foundFile))

                结束使用

                intAantalFotos + = 1

            下一个

            昏暗CmdInsert作为新的OleDbCommand
            昏暗的SQLStatement作为字符串=INSERT INTO tblAlbums(用户ID,createdDate,图片,私人,albumName)VALUES(@userID,现在(),@pictures,@private,@albumName);
            CmdInsert.Connection = dbConn.cn
            CmdInsert.CommandText =的SQLStatement

            CmdInsert.Parameters.AddWithValue(用户ID,CINT(会议(用户ID)))
            CmdInsert.Parameters.AddWithValue(图片,intAantalFotos)
            CmdInsert.Parameters.AddWithValue(私人,chkPrivate.Checked)
            CmdInsert.Parameters.AddWithValue(albumName,txtAlbumNaam.Text)

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

            TempFotos leegmaken

            对于每个foundFile作为字符串在My.Computer.FileSystem.GetFiles(strDirectory)
                File.Delete(foundFile)
            下一个

            pagina herladen

            LblUploadError.Visible = TRUE
            LblUploadError.Text =你的照片已经上传成功!
        结束如果

    结束小组

末级
 

解决方案

问题是,AJAX控件工具包文件上传控件可以(这取决于HTML5特性的浏览器支持)文件上传到服务器的使用隐藏的iFrame。隐藏的iframe引用相同的URL,你页面,这就是为什么加载一次以上,但只隐藏的iframe你的页面。这就是为什么在服务器端处理UploadComplete活动期间,您得到的文本框中具有空值(因为它是真的空值,因为这是这是在iframe中加载页面的状态)。

其中一个问题的解决方案是上传后执行额外的逻辑(这取决于输入的数据)完成。要做到这一点,你可以处理客户端上传完整的事件,并执行回发(或Ajax请求)手动。

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

I am trying to get the fileupload control from the ajax control toolkit to work.

I need to use the uploaded files in my code behind (I use asp.net), This includes unzipping, resizing and putting some data in a database.

The problem I have is that when I use ajaxUpload1_OnUploadComplete, i can't get the text from a textbox on the same page.

When I use a breakpoint, I noticed that the value of the textbox is just "". I have searched a lot and I really can't find a solution for this, so I hoped that someone here might be able to help.

I have pasted my code below, thanks in advance!

.aspx code:

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

code behind:

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

解决方案

The problem is that ajax control toolkit file upload control can upload files to server using hidden iFrame (depending on what HTML5 features browser supports). Hidden iFrame references to the same url as you page and that's why you page loaded once more but only to hidden iframe. That's why on server side during handling UploadComplete event you get that the text box has empty value (because it is really have empty value because this is the state of the page which was loaded in iframe).

One of the solution to your problem is performing additional logic (which depends on entered data) after uploading is complete. To do this you can handle client side upload complete event and perform postback (or ajax request) manually.

Other solution can be setting content of the hidden iFrame elements manually before upload will start. In this case you can get hidden iframe, found necessary HTML elements (like text input in your case) and set it value the same as it was entered by user. But this approach can required extending logic of the ajax control toolkit upload control on client side.

这篇关于AJAX控件工具包文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆