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
salesforce@14salesforce@14 

How to check the file type is csv or not using apex.

Hi,

   I have to check the file is csv or not while uploading it, if not then i want to display the error message, otherwise it gets upload without showing the error "Error:BLOB is not a valid UTF-8 string"

Thanks in Advance

 
Best Answer chosen by salesforce@14
salesforce@14salesforce@14
Hi guys,
 
          Atlast i found the solution.

            I replaced that exception with custom label in catch block like below

if(errorMessage.contains('BLOB is not a valid UTF-8 string'))
            {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR,system.label.Please_Select_Valid_CSV_File));
            }


Thanks.

All Answers

SaranSaran
Hi,

Try using TRY CATCH method,
 
blob docBlob = doc.body;
string blobString;
try
{
blobString = docBlob.toString();
}
catch(Exception e)
    {
        ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL,'Please upload a valid CSV document');
        ApexPages.addMessage(myMsg); 
        return ;   
    }

Hope this might help you.

Thanks
Keyur  ModiKeyur Modi
Hi,
If your file is saved as CSV then it will not show this error "BLOB is not a validaUTF_8 string" so , you can check the blob type if while converting it to string if you will get this error like "BLOB is not a valid UTF-8 string" means file which you are trying to fetch is not saved as CSV . and based on that you can display the message.

Thanks,
Keyur Modi
Jitendra RawatJitendra Rawat
Hi,

If you don't want to use apex coding for checking file type then you have to use java script for checking the file type.
you should refer this link got this 
http://stackoverflow.com/questions/7977084/check-file-type-when-form-submit

If this will help you then please select as beat answer.

Thanks
salesforce@14salesforce@14
Hi guys,

           Thanks for your reply.

why we cannot check the file in if condition using apex?
Is there any way like this to do.
 
salesforce@14salesforce@14
How to display the error message if i try to upload the other files than csv.

Thanks.
Prosenjit Sarkar 7Prosenjit Sarkar 7
HI sai,
I fyou want to show the error mesage use Saran's code and along with that you need to add <apex:PageMessages /> into the <apex:form></apex:form> tag u you have used.
Thanks :)
salesforce@14salesforce@14
Hi,

  I want to show the error message only for the other files not for the csv file.  So how to find out the file is not csv and display the error message in page.

Thanks.
SaranSaran
If you are using in the VF Page you can <apex:PageMessages /> inside your <apex:form>.
salesforce@14salesforce@14
Hi saran,

         I am using <apex:PageMessages /> tag  inside the form only.

Thanks.
 
SaranSaran
As i mentioned in the above comment,

Copy paste that TRY CATCH method in the controller.

Thanks,
salesforce@14salesforce@14
What is this doc.Body in this line blob docBlob = doc.body;
Please explain me briefly..

Thanks.

 
SaranSaran
Doc is the thing which holds the document.

So usually we will convert the body of the documnt to string and then continue with our process.

So i am taking the body of the document which i am attaching as doc.body which is a blob type.

It will be more helpful to identiy the error if you post your code

Thanks
Prosenjit Sarkar 7Prosenjit Sarkar 7
Content of the document @sai
salesforce@14salesforce@14
I mentioned below only the flow  of the corresponding requirement.
 
 public string nameFile{get;set;}
   public Blob contentFile{get;set;}
   nameFile=contentFile.toString();

I did upto this and after that how to check the nameFile is csv or not.

Thanks
SaranSaran
replace the 3rd line like ,
 
try
{
nameFile = contentFile.toString();
}
catch(Exception e)
    {
        ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL,'Please upload a valid CSV document');
        ApexPages.addMessage(myMsg); 
        return ;   
    }

Hope this helps.

Thanks!
salesforce@14salesforce@14
Hi saran,

       Thanks for your reply.

But actually i did like above only.

 
salesforce@14salesforce@14
Hi guys,
 
          Atlast i found the solution.

            I replaced that exception with custom label in catch block like below

if(errorMessage.contains('BLOB is not a valid UTF-8 string'))
            {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR,system.label.Please_Select_Valid_CSV_File));
            }


Thanks.
This was selected as the best answer