function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
devNut!devNut! 

apex:inputFile - throws "Insert failed" on second file being uploaded

I am experimenting with the "<apex:inputFile>" tag trying to create a page where the user can upload one file at a time.  The first file upload works but the second one throws the exception:

Here is the scenario:
1) load file upload page
2) select and upload file
3) user is re-directed to the file upload page
4) select and upload file
5) exception: 
Insert failed. First exception on row 0 with id 01530000000henEAAQ; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]


It's like the context is not being reset when the page is refreshed (see return from doSave() below)
Any input is appreciated.


VF Page

Code:
<apex:page standardController="Document" extensions="TestFileUploadControllerExtension" showHeader="false" sidebar="false">
    <apex:messages />
    <apex:form id="theForm">
      <apex:pageBlock >
          <apex:pageBlockSection >
            <apex:inputFile value="{!document.body}" filename="{!document.name}"/>
            <apex:commandButton value="save" action="{!doSave}"/>
          </apex:pageBlockSection>
       </apex:pageBlock>
     
    </apex:form>
</apex:page>

 
Controller Extension
Code:
public class TestFileUploadControllerExtension {

ApexPages.StandardController controller;


public TestFileUploadControllerExtension(ApexPages.StandardController c)
{
System.debug('@@@@@@@@ TestFileUploadControllerExtension');

this.controller = c;

}

public PageReference doSave()
{
System.debug('@@@@@@@@ doSave');

Document d = (Document) controller.getRecord();
System.debug('@@@@@@@@ document name: '+d.name);

d.folderid = UserInfo.getUserId(); //store in Personal Documents
System.debug('@@@@@@@@ document folder id: '+d.folderid);

insert d;
System.debug('@@@@@@@@ document object id: '+d.id);


System.debug('@@@@@@@@ redirect');
PageReference page = new PageReference('/apex/TestFileUpload');
return page;
}




}

 




Message Edited by devNut! on 11-17-2008 12:35 PM
TehNrdTehNrd
Not sure if this will work but try this:

Code:
PageReference page = new PageReference('/apex/TestFileUpload');
page.setRedirect(true);
return page;

devNut!devNut!
Hey thanks for the reply.

setRedirect did work. I guess that essentially forces the page re-load.
TehNrdTehNrd
edit: this was a sleepy post, see below.


Hmm, I was hoping that would cause the controller to re-instantiate (reload) but I guess it doesn't. (it does)

I haven't played with the inputFile component much but if I have time I'll try to dig deeper a little. I would think it is possible to upload multiple files without having to reload the entire page.


Message Edited by TehNrd on 11-18-2008 09:10 AM
devNut!devNut!
Well the upload of one or more files is working but the page reloads each time like you say.

It seems that reloading of the page is the only way to clear the underlying document variable in the standard controller.

Once an upload happens the following line
Code:
Document d = (Document) controller.getRecord();

returns the uploaded document i.e. the ID field is populated.

TehNrdTehNrd
Oh gosh. I can't read. I am still waking up I guess.

I thought you said it didn't work.
bca321bca321

hi,

 

How to creat a s- control for upload document file