This is a migrated thread and some comments may be shown as answers.

Add a list of files on first load

7 Answers 113 Views
AsyncUpload
This is a migrated thread and some comments may be shown as answers.
max
Top achievements
Rank 1
max asked on 11 Jan 2013, 08:24 AM
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..

7 Answers, 1 is accepted

Sort by
0
Plamen
Telerik team
answered on 15 Jan 2013, 07:34 AM
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.
0
max
Top achievements
Rank 1
answered on 15 Jan 2013, 08:59 AM
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...
0
Plamen
Telerik team
answered on 15 Jan 2013, 01:31 PM
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.
0
max
Top achievements
Rank 1
answered on 16 Jan 2013, 10:59 AM
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





0
max
Top achievements
Rank 1
answered on 24 Jan 2013, 10:14 AM
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
0
Plamen
Telerik team
answered on 29 Jan 2013, 09:31 AM
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.
0
max
Top achievements
Rank 1
answered on 29 Jan 2013, 09:36 AM
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
Tags
AsyncUpload
Asked by
max
Top achievements
Rank 1
Answers by
Plamen
Telerik team
max
Top achievements
Rank 1
Share this question
or