Jasny file upload postedfile always null

58 views Asked by At

Why is the PostedFile property always null even when a file is selected? The file upload appears to be working properly as far as allowing me select a file. But when the submit button is clicked that instantiates the code where the selected file is used, the PostedFile property is null.

This is my ascx code:

<div id="fileUploadDiv" class="row">
    <div class="col-xs-12 push-left">
        <div class="row">
            <div class="col-xs-3 col-md-2 text-left form-group required label-wrapper">
                <asp:Label 
                    ID="Label1" 
                    runat="server" 
                    class="lblFileUpload" 
                    Text="File to upload:" 
                    CssClass="control-label" 
                    EnableViewState="false" />
            </div>
            <div class="col-xs-9 col-xs-10 text-left">
                <div class="fileinput fileinput-new input-group pull-left" data-provides="fileinput">
                    <div class="form-control" data-trigger="fileinput" style="max-width: 30em;"> 
                        <span class="fileinput-filename"></span>
                    </div>
                    <span class="btn btn-file">
                        <span class="fileinput-new">
                            <asp:Button 
                                ID="Button4" 
                                runat="server" 
                                CssClass="button pink inline-narrow pull-right" 
                                Text="Select File" />
                        </span>
                        <span class="fileinput-exists">
                            <asp:Button 
                                ID="Button5" 
                                runat="server" 
                                CssClass="button pink inline-narrow pull-right" 
                                Text="Change" />
                        </span>
                        <input 
                            id="inputUploadFile" 
                            runat="server" 
                            type="file" 
                            name="..." 
                            accept=".jpg, .jpeg, .png, .eps, .pdf, .ai"
                            enctype="multipart/form-data">
                    </span>
                    <a href="#" 
                        class="btn fileinput-exists" 
                        data-dismiss="fileinput">
                        <asp:Button 
                            ID="Button6" 
                            runat="server" 
                            CssClass="button pink inline-narrow pull-right" 
                            Text="Remove" />
                    </a>                             
                </div>                                            
            </div>
        </div>
    </div>
</div>

This is my c# code:

protected void btnAddUpdate_Click(object sender, EventArgs e)
{    
  ...
        byte[] fileData = null;
        using (var binaryReader = new BinaryReader(inputUploadFile.PostedFile.InputStream)) //PostedFile is always null.
        {
            fileData = binaryReader.ReadBytes(inputUploadFile.PostedFile.ContentLength); //PostedFile is always null.
        }
}
1

There are 1 answers

0
ihatemash On

I found the issue. The file upload doesn't work asynchronously. The problem was solved by adding the following two lines of code to the OnInit method:

    ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(this.btnSave);
    Page.Form.Attributes.Add("enctype", "multipart/form-data");

Where this.btnSave is the submit button on the page.