David Lloyd Brookes David Lloyd Brookes - 3 months ago 30
Vb.net Question

Extending asp:controlParameter for use with FileUpload control

Thanks in advance.

Is it possible to extend the

asp:controlParameter
control within an objectDatasource control to accommodate the
asp:fileUpload
control and return a base64 string of the posted file.

for example I would like to be able to do this (key area to note is the insert parameter):

<asp:listview runat="server" datasourceID="objDS">
<insertTemplate>
<asp:fileupload id="fu" runat="server" />
<asp:button id="submit" runat="server" text="Upload" commandName="INSERT" />
</insertTemplate>
</asp:listview>

<asp:objectDatasource id="objDS" runat="server" ...>
<insertParameters>
<asp:fileuploadControlParameter controlID="fu" type="string" name="base64" />
</insertParameters>
</asp:objectDatasource>

Answer

It's become apparent I've been approaching this the wrong way, I've simply extended the asp:fileupload control that allows me to bind the new property of base64 to the existing asp:parameter control of the objectDatasource.

Public Class fupload
    Inherits FileUpload

    Public ReadOnly Property base64 As String
        Get
            Return encodeBase64()
        End Get
    End Property

    Public Function encodeBase64() As String
        Dim iLen As Integer = MyBase.PostedFile.ContentLength
        Dim btArr As Byte() = New Byte(iLen - 1) {}
        MyBase.PostedFile.InputStream.Read(btArr, 0, iLen)
        Dim b64Str As String = String.Format("data:{0};base64,{1}",
                                             MyBase.PostedFile.ContentType,
                                             Convert.ToBase64String(btArr))
        Return b64Str
    End Function
End Class

example usage:

<asp:listview runat="server" datasourceID="objDS">
 <insertTemplate>
  <asp:fileupload id="fu" runat="server" base64='<%# bind("base64")' />
  <asp:button id="submit" runat="server" text="Upload" commandName="INSERT" />
 </insertTemplate>
</asp:listview>

<asp:objectDatasource id="objDS" runat="server" ...>
 <insertParameters>
  <asp:Parameter type="string" name="base64" />
 </insertParameters>
</asp:objectDatasource>