[SalesForce] Attachment name/file not being set, set null

Ive been trying to add code to add a an attachment to my custom object. However the file, and filename keep showing up null (if I wrap "if file != null" around the attachment insert it bypasses it and inserts the object correctly), Im assuming it not being set, but I cant tell why.
Relevent controller code:

public Blob file {get;set;} 
public String UploadedfileName {get;set;} 
public ApexPages.StandardController std;

public myController(ApexPages.StandardController stdController) {
    std = stdController;
}

public void submit() {
MapRequest__c cont=(MapRequest__c) std.getRecord();
insert cont;
Attachment att = new Attachment(); 
att.Body = file; 
att.Name = UploadedfileName; 
att.ParentId = cont.Id; 
att.Description = 'file uploaded.'; 
insert att; 

}

Relevant VF code:

<body>
    <apex:form>

    <apex:pageBlock title="Map Request Form" id="entireForm">
        <apex:pageBlockButtons id="buttons">
            <apex:commandButton status="pStatus" action="{!submit}"
                immediate="false" value="Submit" />
        </apex:pageBlockButtons>
      <apex:pageBlockSection columns="1" id="secondSection">
            <apex:pageBlockSection collapsible="true" columns="2"
                title="2nd Map Type" rendered="{!display2nd}">
                <apex:outputLabel value="Upload file 1" />  
                 <apex:inputFile value="{!file}" id="fileupload" fileName="{!UploadedfileName}"/> 
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="2nd Set Of Questions" for="dueDate" />
                    <apex:inputfield value="{!MapRequest__c.Due_Date__c}" id="dueDate" />
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
        </apex:pageBlockSection>
     </apex:PageBlock>
    </apex:form>
 </body>

Note: both above codes are snippets and are likely missing a lot I just tried to grab relevant portions of the code and put them together in a meaningful format.

~~~~~EDIT~~~~~~~~~~~~~~~~~~~

I just found out that if I select a file and hit submit with one of the required fields left blank (which causes the page to trigger the whole "Error: required field missing" message box and stops submission. Then I fill in that required field and submit again, the submit will be successful and the file will upload.
This implies hat the first time I hit submit it is setting the file's fields and the second time I hit submit its actually submitting. Any reason why it would do that??

Best Answer

Ok I found the issue, apparently inputFiles require some sort of page refresh to read the data properly. To do this I simply changed

<apex:form>

to

<apex:form enctype="multipart/form-data">

Not really sure what this changes at all but it makes it work.

Related Topic