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
Alex KirbyAlex Kirby 

Uploading document using visualforce file extension?

Hi all,

I have a controller and a vf page to upload documents to a folder in our community. The file uploads successfully but the file extension is not captured in the process causing issues when trying to open the file.

VF:
 
<apex:pageBlock >
    
    
    <apex:pageBlockSection showHeader="false" columns="2" id="block1">

        <apex:pageBlockSectionItem >
          <apex:outputLabel value="File Name" for="fileName"/>
          <apex:inputText value="{!document.name}" id="fileName"/>
        </apex:pageBlockSectionItem>

        <apex:pageBlockSectionItem >
          <apex:outputLabel value="File" for="file"/>
          <apex:inputFile value="{!document.body}" filename="{!document.name}"  id="file"/>
        </apex:pageBlockSectionItem>

        <apex:pageBlockSectionItem >
          <apex:outputLabel value="Description" for="description"/>
          <apex:inputTextarea value="{!document.description}" id="description"/>
        </apex:pageBlockSectionItem>

        <apex:pageBlockSectionItem >
          <apex:outputLabel value="Keywords" for="keywords"/>
          <apex:inputText value="{!document.keywords}" id="keywords"/>
        </apex:pageBlockSectionItem>

      </apex:pageBlockSection>

Controller: 
 
public with sharing class qmtDocuments {

    List<Folder> fld = [Select id from Folder where DeveloperName ='qmtDocumentsFolder' limit 1];

    public List<Document> getDocuments() {
        return [select id, Name, Description, ContentType from Document Where FolderId =:fld];
    }
    
 
  public Document document {
    get {
      if (document == null)
        document = new Document();
      return document;
    }
    set;
  }
 
  public PageReference upload() {
    
    document.AuthorId = UserInfo.getUserId();
    document.FolderId = string.valueof(fld[0].get('id')); // put it in running user's folder
    document.name = document.name;
    	
    try {
      insert document;
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading file'));
      return null;
    } finally {
      document.body = null; // clears the viewstate
      document = new Document();
   
    }
 
    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'File uploaded successfully'));
    return null;
  }
 
}

Result:

User-added image

The top file I entered using standard salesforce in the backend as a test.

Any help is appreciated, cheers.
Best Answer chosen by Alex Kirby
Alex KirbyAlex Kirby
I resolved this by removing the 
<apex:pageBlockSectionItem >
          <apex:outputLabel value="File Name" for="fileName"/>
          <apex:inputText value="{!document.name}" id="fileName"/>
        </apex:pageBlockSectionItem>

from the above, this uploads the filename with the extensions attached using <apex:inputfile>. I can't expose this functionality to the community though as I am unable to give create permissions to community users for documents

All Answers

KaranrajKaranraj
Add ContentType in the <apex:inputFile> tag.
Try the updated visualforce code below
<apex:pageBlock >
    
    <apex:pageBlockSection showHeader="false" columns="2" id="block1">

        <apex:pageBlockSectionItem >
          <apex:outputLabel value="File Name" for="fileName"/>
          <apex:inputText value="{!document.name}" id="fileName"/>
        </apex:pageBlockSectionItem>

        <apex:pageBlockSectionItem >
          <apex:outputLabel value="File" for="file"/>
          <apex:inputFile value="{!document.body}" filename="{!document.name}" contentType="{!document.Type}"
  id="file"/>
        </apex:pageBlockSectionItem>

        <apex:pageBlockSectionItem >
          <apex:outputLabel value="Description" for="description"/>
          <apex:inputTextarea value="{!document.description}" id="description"/>
        </apex:pageBlockSectionItem>

        <apex:pageBlockSectionItem >
          <apex:outputLabel value="Keywords" for="keywords"/>
          <apex:inputText value="{!document.keywords}" id="keywords"/>
        </apex:pageBlockSectionItem>

      </apex:pageBlockSection>

 
Alex KirbyAlex Kirby
Thanks, I tried this before but I gave it another go and it didn't work. I also added document.contentType = document.type; into the controller and this didn't work either.
Alex KirbyAlex Kirby
I resolved this by removing the 
<apex:pageBlockSectionItem >
          <apex:outputLabel value="File Name" for="fileName"/>
          <apex:inputText value="{!document.name}" id="fileName"/>
        </apex:pageBlockSectionItem>

from the above, this uploads the filename with the extensions attached using <apex:inputfile>. I can't expose this functionality to the community though as I am unable to give create permissions to community users for documents
This was selected as the best answer