I'm trying to upload a file in VB.NET using an HtmlInputFile, but everytime I upload, the HtmlInputFile's PostedFile is Nothing.
Here is my ASP.NET code:
<form id="Form1" method="post" runat="server" enctype="multipart/form-data"> <asp:DataGrid id="dgTitles" runat="server" OnUpdateCommand="dgUpdate" DataKeyField="ID"> <Columns> <asp:TemplateColumn HeaderText="Title" > <ItemTemplate> <asp:Label ID="lTitle" Runat="server"> <%# DataBinder.Eval(Container, "DataItem.Title" )%> </asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="tTitle" Runat="server" text='<%# DataBinder.Eval(Container, "DataItem.Title" )%>'> </asp:TextBox> </EditItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="Image"> <EditItemTemplate> <input id='fuEditImage' type="file" name="fuEditImage" runat="server" enctype="multipart/form-data" /> </EditItemTemplate> </asp:TemplateColumn <asp:EditCommandColumn UpdateText="Update"> </asp:EditCommandColumn> </Columns> </asp:DataGrid> </form>
Here is my VB.NET code:
Sub dgUpdate(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
Try
Dim Title = CType(e.Item.FindControl("tTitle"), TextBox).Text
Dim fuUpload As System.Web.UI.HtmlControls.HtmlInputFile = CType(e.Item.FindControl("fuEditImage"), _
System.Web.UI.HtmlControls.HtmlInputFile)
If Not fuUpload.PostedFile Is Nothing Then 'This always = false
If Not fuUpload.PostedFile.FileName = "" Then
UploadImage(fuUpload)
End If
End If
Catch ex As Exception
'Handle Exception here
End Try
End Sub
When I upload a file, fuUpload is not Nothing, but fuUpload.PostedFile is.
The inputted "Title" text is recognized no problem.
I've also tried with FileUpload object and tag, instead of input tag and HtmlInputFile-- same result.
Here is the output of ?fuUpload when I use an input tag & HtmlInputFile:
{System.Web.UI.HtmlControls.HtmlInputFile} Accept: "" AppRelativeTemplateSourceDirectory: "~/app/admin/points/" Attributes: {System.Web.UI.AttributeCollection} BindingContainer: {System.Web.UI.WebControls.DataGridItem} ClientID: "dgTitles__ctl4_fuEditImage" Controls: {System.Web.UI.EmptyControlCollection} Disabled: False EnableTheming: True EnableViewState: True ID: "fuEditImage" MaxLength: -1 Name: "dgTitles:_ctl4:fuEditImage" NamingContainer: {System.Web.UI.WebControls.DataGridItem} Page: {ASP.app_admin_points_pointtitles_aspx} Parent: {System.Web.UI.WebControls.TableCell} PostedFile: Nothing Site: Nothing Size: 45 SkinID: "" Style: {System.Web.UI.CssStyleCollection} TagName: "input" TemplateControl: {ASP.app_admin_points_pointtitles_aspx} TemplateSourceDirectory: "/community/app/admin/points" Type: "file" UniqueID: "dgTitles:_ctl4:fuEditImage" Value: "" Visible: True
Here is the output of ?fuUpload if I'm using a FileUpload object and tag:
{System.Web.UI.WebControls.FileUpload} AccessKey: "" AppRelativeTemplateSourceDirectory: "~/app/admin/points/" Attributes: {System.Web.UI.AttributeCollection} BackColor: "{Name=0, ARGB=(0, 0, 0, 0)}" BindingContainer: {System.Web.UI.WebControls.DataGridItem} BorderColor: "{Name=0, ARGB=(0, 0, 0, 0)}" BorderStyle: NotSet {0} BorderWidth: {System.Web.UI.WebControls.Unit} ClientID: "dgTitles__ctl4_fuEditImage" Controls: {System.Web.UI.ControlCollection} ControlStyle: {System.Web.UI.WebControls.Style} ControlStyleCreated: True CssClass: "" Enabled: True EnableTheming: True EnableViewState: True FileBytes: {Length=0} FileContent: {System.IO.Stream.NullStream} FileName: "" Font: {System.Web.UI.WebControls.FontInfo} ForeColor: "{Name=0, ARGB=(0, 0, 0, 0)}" HasAttributes: False HasFile: False Height: {System.Web.UI.WebControls.Unit} ID: "fuEditImage" NamingContainer: {System.Web.UI.WebControls.DataGridItem} Page: {ASP.app_admin_points_pointtitles_aspx} Parent: {System.Web.UI.WebControls.TableCell} PostedFile: Nothing Site: Nothing SkinID: "" Style: {System.Web.UI.CssStyleCollection} TabIndex: 0 TemplateControl: {ASP.app_admin_points_pointtitles_aspx} TemplateSourceDirectory: "/community/app/admin/points" ToolTip: "" UniqueID: "dgTitles:_ctl4:fuEditImage" Visible: True Width: {System.Web.UI.WebControls.Unit}
Can anyone tell me what I'm doing wrong?
Why is PostedFile always nothing?
Thanks!