Add a list of files on first load

8 posts, 0 answers
  1. max
    max avatar
    42 posts
    Member since:
    Nov 2007

    Posted 11 Jan 2013 Link to this post

    Hi, is there a way to load the AsyncUpload control with a list of files?

    Your sample http://demos.telerik.com/aspnet-ajax/asyncupload/examples/overview/defaultcs.aspx is ok for a new mail, but when you forward a mail you should keep the attachments..
  2. Plamen
    Admin
    Plamen avatar
    2731 posts

    Posted 15 Jan 2013 Link to this post

    Hello Max,

     
    Such behavior can not be achieved because of browser security limitations that does not allow it.

    Kind regards,
    Plamen
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. max
    max avatar
    42 posts
    Member since:
    Nov 2007

    Posted 15 Jan 2013 Link to this post

    Even uploading the files server-side and filling the client state with the correct values?
    The client state it's simply a json with a base64 data...
  5. Plamen
    Admin
    Plamen avatar
    2731 posts

    Posted 15 Jan 2013 Link to this post

    Hi,

     
    We are not aware of a current workaround for the issue but if you in your further examinations succeed in achieving this please let us know of the exact way to do it so we could help other users as well.

    Regards,
    Plamen
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  6. max
    max avatar
    42 posts
    Member since:
    Nov 2007

    Posted 16 Jan 2013 Link to this post

    Public Class AsyncUpload
        Inherits Telerik.Web.UI.RadAsyncUpload
     
        Public Property ClientState As String
     
        Private hfClientState As New HiddenField
     
        Public Sub addFilesFromFolder(folder As String)
            Dim tempClientState As String = "{""isEnabled"":""true"", ""uploadedFiles"": ["
            Dim di As New System.IO.DirectoryInfo(folder)
            Dim i As Integer = 0
            For Each fi In di.GetFiles()
                If fi.Name <> "RadUploadTestFile" Then
                    Dim t As System.Reflection.Assembly = System.Reflection.Assembly.GetAssembly(GetType(Telerik.Web.UI.UploadedFileInfo))
                    Dim meta As String = "{""TempFileName"":""" + fi.Name + """,""AsyncUploadTypeName"":""Telerik.Web.UI.UploadedFileInfo, " + t.FullName + """}"
                    meta = Convert.ToBase64String(Encoding.ASCII.GetBytes(meta))
                    Dim ct As String = MimeTypeUtil.getMimeFromFile(fi.FullName)
                    tempClientState += "{""fileInfo"":{""FileName"":""" + fi.Name + """,""ContentType"":""" + ct + """,""ContentLength"":" & fi.Length & ",""Index"":" & i & "},""metaData"":""" + meta + """},"
                    i += 1
                End If
            Next
            tempClientState += "]}"
            tempClientState = tempClientState.Replace("},]", "}]")
            Me.ClientState = tempClientState
        End Sub
     
        Public Overrides Sub RenderClientStateField(writer As HtmlTextWriter)
     
            If Not String.IsNullOrEmpty(ClientState) Then
     
                hfClientState.ID = Me.ClientID + "_ClientState"
     
                hfClientState.Value = ClientState
                hfClientState.RenderControl(writer)
            Else
                MyBase.RenderClientStateField(writer)
            End If
     
        End Sub
     
    End Class

    I wrore this test class

    Assumed folder is the same as AsyncUpload.TemporaryFolder

    The files list is loaded correctly but the postback fail with this exception:

    Uncaught Sys.WebForms.PageRequestManagerServerErrorException: Sys.WebForms.PageRequestManagerServerErrorException: Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster. Telerik.Web.UI.WebResource.axd:15





  7. max
    max avatar
    42 posts
    Member since:
    Nov 2007

    Posted 24 Jan 2013 Link to this post

    ok. this is the solution!
    The code is bad written (RenderClientStateField might be buggy), but it's working

    Public Class AsyncUpload
        Inherits Telerik.Web.UI.RadAsyncUpload
     
        Public Property forcedClientState As String
     
        Public Sub setTemporaryFolderAndLoadFiles(folder As String)
            Me.TemporaryFolder = folder
            Dim di As New System.IO.DirectoryInfo(folder)
            Dim i As Integer = 0
            Dim uploadedFiles As New List(Of Object)
            Dim s As New JavaScriptSerializer
             For Each fi In di.GetFiles()
                Dim t As System.Reflection.Assembly = System.Reflection.Assembly.GetAssembly(GetType(Telerik.Web.UI.UploadedFileInfo))
                Dim meta As String = "{""TempFileName"":""" + fi.Name + """,""AsyncUploadTypeName"":""Telerik.Web.UI.UploadedFileInfo, " + t.FullName + """}"
     
                Dim formatter As LosFormatter = New LosFormatter(True, "string")
                Dim output As New StringWriter
                formatter.Serialize(output, meta)
                meta = output.ToString
                Dim ct As String = MimeTypeUtil.getMimeFromFile(fi.FullName)
                uploadedFiles.Add(New With {.fileInfo = New Telerik.Web.UI.UploadedFileInfo With {.FileName = fi.Name, _
                                                                  .ContentType = ct, _
                                                                  .ContentLength = fi.Length.ToString.ToLower, _
                                                                  .Index = i.ToString}, _
                                            .metaData = meta
                                           })
                i += 1
            Next
     
            Dim ss = s.Serialize(New With {.isEnabled = Me.IsEnabled.ToString, .uploadedFiles = uploadedFiles})
            Me.forcedClientState = ss.Replace("""", """)
     
        End Sub
     
     
        Public Overrides Sub RenderClientStateField(writer As HtmlTextWriter)
     
            If Not String.IsNullOrEmpty(forcedClientState) Then
                Dim old As String = ""
                Using sw As New System.IO.StringWriter()
                    Using w As New HtmlTextWriter(sw)
                        MyBase.RenderClientStateField(w)
                    End Using
                    old = sw.ToString()
                End Using
                writer.Write(old.Replace("/>", "value=""" + forcedClientState + """ />"))
            Else
                MyBase.RenderClientStateField(writer)
            End If
        End Sub
     
    End Class
  8. Plamen
    Admin
    Plamen avatar
    2731 posts

    Posted 29 Jan 2013 Link to this post

    Hi Massimo,

     
    Thank you for sharing the code with us.

    Would you please provide the code for "MimeTypeUtil.getMimeFromFile" as well so we could be able to test it properly and share some feedback concerning it?

    Regards,
    Plamen
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  9. max
    max avatar
    42 posts
    Member since:
    Nov 2007

    Posted 29 Jan 2013 Link to this post

    there are many ways to get the mime type, this is my code:

    Imports System.Runtime.InteropServices
    Imports System.IO
     
    Public Class MimeTypeUtil
     
        <DllImport("urlmon.dll", CharSet:=CharSet.Auto)> _
        Private Shared Function FindMimeFromData(pBC As System.UInt32, <MarshalAs(UnmanagedType.LPStr)> pwzUrl As System.String, <MarshalAs(UnmanagedType.LPArray)> pBuffer As Byte(), cbSize As System.UInt32, <MarshalAs(UnmanagedType.LPStr)> pwzMimeProposed As System.String, dwMimeFlags As System.UInt32, _
        ByRef ppwzMimeOut As System.UInt32, dwReserverd As System.UInt32) As System.UInt32
        End Function
     
        Public Shared Function getMimeFromFile(filename As String) As String
            If Not File.Exists(filename) Then
                Throw New FileNotFoundException(filename & " not found")
            End If
     
            Dim buffer As Byte() = New Byte(255) {}
            Using fs As New FileStream(filename, FileMode.Open)
                If fs.Length >= 256 Then
                    fs.Read(buffer, 0, 256)
                Else
                    fs.Read(buffer, 0, CInt(fs.Length))
                End If
            End Using
            Try
                Dim mimetype As System.UInt32
                FindMimeFromData(0, Nothing, buffer, 256, Nothing, 0, _
                    mimetype, 0)
                Dim mimeTypePtr As System.IntPtr = New IntPtr(mimetype)
                Dim mime As String = Marshal.PtrToStringUni(mimeTypePtr)
                Marshal.FreeCoTaskMem(mimeTypePtr)
                Return mime
            Catch e As Exception
                Return "unknown/unknown"
            End Try
        End Function
     
    End Class
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017