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
@login.ax974@login.ax974 

How to get the file name in the apex:inputFile tag

Hi,

 

 Is there a way to capture the fileName in the apex:inutFile after choosing the file and default it to the value in the apex:inputField? I am doing below (snippet) but i get the file name as undefined all the time - any idea why or any other better way of doing it?

 

<script>

 function defaultName()

{
   var fileName2 = document.getElementById("thePage:theForm:fileInput");
   var documentName = document.getElementById("thePage:theForm:docName");
   documentName.value = fileName2.value;  ------> I get undefined here?? How do i get the fileName???
   return true;
}

</script>

<apex:inputFile value="{!con}" contentType="{!type}" fileName="{!name}" id="fileInput" onchange="javascript&colon;defaultName()"/>

<apex:inputField value="{!docName}" id="docName"/>

 

thanks so much.

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

 

Try the below code snippet as reference:

 

============== Vf page=====================

<apex:page controller="UploadDocument" id="p1">
<script>
function check(obj_inpf)
{
alert('_______obj_inpf_________'+obj_inpf.value);
}
</script>
  <apex:form id="f1">
      <apex:pageBlock id="pb1">
          <apex:pageBlockSection id="pbs1" >
            <apex:inputFile id="if1" value="{!file1}" fileName="{!fname}" onchange="check(this)"  />
            <apex:commandButton value="Save" action="{!saveDoc1}"/>
          </apex:pageBlockSection>
       </apex:pageBlock>
    </apex:form>
</apex:page>


============= Apex controller ======================

public class UploadDocument {

public blob file1{get;set;}
public String fname{get;set;}

public UploadDocument()
{
    
   
}

public void saveDoc1()
{
    Document d= new Document();
    d.name=fname;
    d.body=file1;
    d.AuthorId = UserInfo.getUserId();
    d.FolderId = UserInfo.getUserId();
   // d.contenttype='txt';
  //  d.type='text/plain';
    d.IsPublic = true;
    

system.debug('_____________________d.name_____________________________'+d.name);
system.debug('_____________________fname_____________________________'+fname);
insert d;

}
}

 

 Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

All Answers

abhishektandon2abhishektandon2

Hi , there is no need to use javascript to get the file nam eof the uploaded file.

 

you can use below snippet to get that 

 

 <apex:inputFile value="{!contentFile}" filename="{!nameFile}"  /> 

 

create nameFile as a string variable in you r contrioller, you will get the name of file in that.

 

public with sharing class MyController {

 

// To hold the name of file
public String nameFile { get; set; }

 

}

 

Please accept this as a solution if it solve your issue

Navatar_DbSupNavatar_DbSup

Hi,

 

Try the below code snippet as reference:

 

============== Vf page=====================

<apex:page controller="UploadDocument" id="p1">
<script>
function check(obj_inpf)
{
alert('_______obj_inpf_________'+obj_inpf.value);
}
</script>
  <apex:form id="f1">
      <apex:pageBlock id="pb1">
          <apex:pageBlockSection id="pbs1" >
            <apex:inputFile id="if1" value="{!file1}" fileName="{!fname}" onchange="check(this)"  />
            <apex:commandButton value="Save" action="{!saveDoc1}"/>
          </apex:pageBlockSection>
       </apex:pageBlock>
    </apex:form>
</apex:page>


============= Apex controller ======================

public class UploadDocument {

public blob file1{get;set;}
public String fname{get;set;}

public UploadDocument()
{
    
   
}

public void saveDoc1()
{
    Document d= new Document();
    d.name=fname;
    d.body=file1;
    d.AuthorId = UserInfo.getUserId();
    d.FolderId = UserInfo.getUserId();
   // d.contenttype='txt';
  //  d.type='text/plain';
    d.IsPublic = true;
    

system.debug('_____________________d.name_____________________________'+d.name);
system.debug('_____________________fname_____________________________'+fname);
insert d;

}
}

 

 Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

This was selected as the best answer
Aliaksandr VishneuskiAliaksandr Vishneuski
The issue is not to get a file name from the controller, the challenge is to how to initially set file name to the controller to be able after that to reference from markup.