Formview FileUpload and ObjectDataSource

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

I want do my insert to a database using a formview bound to an
objectdatasource. I also want to upload a file. I'm quite new to and this is
just test code but Is there any way I can pass the file as a byte array and
manipulate it in my business object. Currently what I have is this, but my
byte array in my business object is nothing. It's like it's not passed the
file as a stream. I think I am not binding the FileUpload to the
insertparameter but I don't know how to fix it. Regards, Chris.


<asp:FormView ID="frmvwCustomer" runat="server" DataSourceID="objdsCustomer"
DefaultMode="Insert" AllowPaging="True">

<InsertItemTemplate>

Name<asp:TextBox ID="txtname" runat="server"></asp:TextBox><br />

Email<asp:TextBox ID="txtemail" Text="<%# Bind('strEmail') %>"
runat="server"></asp:TextBox><br />

CV<asp:FileUpload ID="CV" runat="server" /><br />

&nbsp;<asp:Button ID="Button1" runat="server" CommandName="Insert"
Text="Button" />

</InsertItemTemplate>

</asp:FormView>


</div>

<asp:ObjectDataSource ID="objdsCustomer" runat="server"
InsertMethod="createcustomer"

SelectMethod="getcustomer_list" TypeName="Customer">

<InsertParameters>

<asp:Parameter Name="strName" Type="Object" />

<asp:Parameter Name="strEmail" Type="Object" />

<asp:Parameter Name="CV" Type="Object" />

</InsertParameters>

</asp:ObjectDataSource>





Public Sub createcustomer(ByVal strname As String, ByVal strEmail As String,
ByVal cv() As Byte)

End Sub
 
I 've got something like that working. My problem is that I want to put all
logic, and that would include manipulating the uploaded file in a business
object, preferably one method. Putting that code on the page with the
control doesn't seem very 3 tier, which is what I was aiming for. Really I
want to grab the stream rather than file the control.

I got something like this working ages ago but I've lost the code. I seem to
remember being able to specify the type as filebytes though.
 
I may be missing your objective, but it seems that a formview, and associated upload control, would be dealt with through an event
handler (code beside or in page) rather than a business object layer...
 
What I mean is that I don't want to put business logic about e.g. where an
object is saved in the presentation layer. If this is something that you
can't really acheive with the formview/objectdatasorce I have a work around
it's not very good but I have posted it. A cleaner solution is to bind the
the stream to byte array and use that in the business layer.

My slightly hacky work around is to tell pass the business object the name
of the formview and fileupload and it find it on the page. It works but I
don't like it....



<form id="form1" runat="server" enctype="multipart/form-data">
<div>
&nbsp;<asp:FormView ID="frmvwCustomer" runat="server"
DataSourceID="objdsCustomer" DefaultMode="Insert" AllowPaging="True">
<InsertItemTemplate>
Name<asp:TextBox ID="txtname" runat="server"></asp:TextBox><br />
Email<asp:TextBox ID="txtemail" Text="<%# Bind('strEmail') %>"
runat="server"></asp:TextBox><br />
CV<asp:FileUpload ID="CV" runat="server" /><br />
&nbsp;<asp:Button ID="Button1" runat="server" CommandName="Insert"
Text="Button" />
</InsertItemTemplate>
</asp:FormView>
</div>
<asp:ObjectDataSource ID="objdsCustomer" runat="server"
InsertMethod="createcustomer"
SelectMethod="getcustomer_list" TypeName="Customer">
<InsertParameters>
<asp:Parameter Name="strname" Type="String" />
<asp:Parameter Name="strEmail" Type="String" />
<asp:Parameter Name="formview" DefaultValue="frmvwCustomer" Type="String" />
<asp:Parameter Name="upload" DefaultValue="CV" Type="String" />
</InsertParameters>
</asp:ObjectDataSource>
</form>


Public Sub createcustomer(ByVal strname As String, ByVal strEmail As String,
ByVal formview As String, ByVal upload As String)
Dim page As Page = HttpContext.Current.CurrentHandler
Dim frmvw As FormView = CType(page.FindControl(formview), FormView)
Dim fileup As FileUpload = CType(frmvw.FindControl(upload), FileUpload)
Dim cv() As Byte = fileup.FileBytes
HttpContext.Current.Response.Write(cv)
HttpContext.Current.Response.ContentType = "application/pdf"
HttpContext.Current.Response.AddHeader("Content-Disposition",
"attachment;filename = test.pdf")
HttpContext.Current.Response.BinaryWrite(cv)
HttpContext.Current.Response.End()
End Sub
 
Back
Top