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
Kenji778Kenji778 

Create file from file input upload

Hey all,

This is similar to my thread from last night, but the question has changed a bit. So I have a visualforce page with a regular old file input. When the form is submitted, it posts to a visualforce page attached to an apex controller. When the apex controller evaluates the file form field, it gets a reference to a temporary file, like 

/home/sfdc/salesforce/sfdc/jsp/form/form1091191222.tmp

How would I take that file reference and do something with it? Like actually get the contents of it so I can create an attachment. Or extract the name of it? Any thoughts are much appreciated as I am a bit stuck. Thanks ahead of time! 

bob_buzzardbob_buzzard

You can create an attachment from a VF page in a more straightforward fashion.  Here's an example from my dev org:

 

Controller:

 

public class PositionExtensionController {
 
    ApexPages.StandardController stdController;
 
    public Attachment attachment {
        get {
            if (attachment == null) attachment = new Attachment();
            return attachment;
        }
 
        set;
    }
 
    public PositionExtensionController (ApexPages.StandardController stdController)
    {
        this.stdController = stdController;
    }
 
    public PageReference save() {
        attachment.parentid = stdController.getRecord().id;
        insert attachment;
       
        PageReference page = ApexPages.currentPage();
        page.setRedirect(true);
        return page;
     }
}

Page:

 

<apex:page standardController="Position__c" extensions="PositionExtensionController">
<apex:messages />
    <apex:form id="theForm">
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:inputFile value="{!attachment.body}" filename="{!attachment.name}"/>
                <apex:commandButton value="Save" action="{!save}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
    <apex:pageBlock >
        <apex:repeat value="{!Position__c.attachments}" var="attachment">
            <apex:image height="200px" value="{!URLFOR($Action.Attachment.Download, attachment.Id)}"/><p/>
        </apex:repeat>
    </apex:pageBlock>
</apex:page>

 

As the file input is backed by fields from the attachment, its ready to go once the postback is complete and the save method is invoked.

 

Kenji778Kenji778

Hm, well the tricky part is here, I am trying to build a multiple file uploader, so I can't really use the apex:inputFile control I don't think. How my uploader works is that it uses an HTML5 multiple file uploader and posts each file 1 by 1 to my VF page that should take the content and create a file from it. Does the apex:inputFile support multiple files?

bob_buzzardbob_buzzard

Not that I'm aware of - I tend to use an apex:repeat tag and have multiple apex:inputfile components when I need more than one.

bob_buzzardbob_buzzard

To return to your original point, I can't think of anything that I've seen that would allow you to access a file on the server. There was an entry in the force.com blog email that I received today around sending binary data to apex endpoints which may be of use.

 

Frustratingly, the link through to the developerforce site generates an empty page.

Kenji778Kenji778

In response to your idea about using a repater for multile form uploads normally I'd take that as on okay solution, but the spec I am developing to is fairly demanding. I don't really get to use that as a workaround. There must be some way to do this, I just don't know what it is yet.

 

In am intruiged about the idea of sending binary/base64 content raw. If i could just send the content of the files, and reconstruct them in apex, that would work. Not quite sure what the process for that would be though.

Kenji778Kenji778

Aight, well I am getting a bit desperat to come up with some idea. This is for a contest, and if I win I'm willing to cut someone in on the profits (probably about $200). If you are willing to work with me a bit and come up with a plan on how to do this, and we win, there is cash in it. I have all the rest of the hard work taken care of, just need to figure out how to transfer data from my uploader interface to the apex class to create the attachment. Let me know if you are interested.

Kenji778Kenji778

Really there isn't anything special about the file input that visualforce makes, at least as far as I can tell. One would assume that it would be possible to essentially recreate the functionality. All the apex input:file does is render a fairly standard file input. The magic for referencing the file has to be in the apex I think....

Kenji778Kenji778

Iterating through all the data passed to my controller I see this

 

id: a0GE0000000TVdL

File Data 
files: /home/sfdc/salesforce/sfdc/jsp/form/form741011313.tmp 
files.content-type: image/jpeg 
files.file: /home/sfdc/salesforce/sfdc/jsp/form/form741011313.tmp 
files.filename: 36436_1142380217556_1769004737_280864_816823_n.jpg 

 

So it seems as though the upload is actually happening, it just is storing the file in a location that I can't seem to get to. Anyone have any idea where the location would be? I could make a callout to it read it as base64 and create my attachment if i could just access that **bleep** path.

bob_buzzardbob_buzzard

Unfortunately I don't have a solution, but I can hopefully shed a bit of light on how file uploads work.  You are correct in that the file is stored in a temporary location, but I doubt that its apex that is handling this.

 

As jsp appears in the file path, its more likely that there is a dedicated servlet that processes uploaded files and stores them in a temp area on the server.  At that level, you'd have access to the contents of the file and could do whatever is needed.  However, in this case you are disconnected from the actual server environment so you don't have a way to open that file and read the contents of it.

 

 

chris_parxchris_parx

I have exactly the same problem... I have to get the file encoded in binary data and not in base64 to send it to a webservice. I had also the idea trying to access to the file saved on the server but I don't find how to achieve that ... I tried also to convert from base64 to literal byte but I don't find a way. I even tried in javascript as it's quite easy to convert, but data got corrupted anyway...

really frustrating issue.

mike.letullemike.letulle

Wow I wish I had found this thread days ago as i got to the same hurdle. Can anyone tell me how I can access that tmp file on the server side from apex code?

ramakrishna.kini.ul1.3928312390456768E12ramakrishna.kini.ul1.3928312390456768E12
Hi,

Is there any way we can retain the uploaded file if we are navigating between multiple pages.

Like upload once but not save.