2013-04-28 82 views
3

我想從控制fileupload控制工作。ajax控制工具包文件上傳

我需要在我的代碼中使用上傳的文件(我使用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 

回答

3

的問題是,AJAX控件工具包文件上傳控件可以使用隱藏的iFrame(取決於文件上傳到服務器瀏覽器支持哪些HTML5功能)。隱藏iFrame引用與您網頁相同的網址,這就是爲什麼您的網頁再次加載,但僅限於隱藏的iframe。這就是爲什麼在服務器端處理UploadComplete事件時,你會發現文本框有空值(因爲它實際上是空值,因爲這是在iframe中加載的頁面的狀態)。

上傳完成後,您的問題的解決方案之一是執行額外的邏輯(這取決於輸入的數據)。爲此,您可以處理客戶端上傳完成事件並手動執行回發(或ajax請求)。

其他解決方案可以在開始上傳之前手動設置隱藏的iFrame元素的內容。在這種情況下,您可以隱藏iframe,找到必要的HTML元素(如您的案例中的文本輸入)並將其值設置爲與用戶輸入的值相同。但是這種方法可能需要在客戶端擴展ajax控件工具包上傳控件的邏輯。

+0

謝謝你的擴張,我真的不知道如何filupload工作直到我讀它,我現在明白錯誤 – 2013-04-29 15:56:40

+1

@Joris_H你怎麼最終解決這個問題?你可以發佈代碼嗎?謝謝。 – user713813 2013-08-10 06:58:27