Only csv file should be imported

Hi Experts,

In this code, only CSV file should be accepted but its taking even the .png and other image file and so on . How can i make the below code only for csv.

AsciiStreamIo file;
Array fileLines;
FileUploadTemporaryStorageResult fileUpload;
Counter counter = 0;

    boolean                             header = true;
    #OCCRetryCount;

    try
    {
        //Upload a file
        fileUpload  = File::GetFileFromUser() as FileUploadTemporaryStorageResult;
        file        = AsciiStreamIo::constructForRead(fileUpload.openResult());
             
        if (file)
        {
            if (file.status())
            {
                throw error("@SYS52680");
                
            }
            file.inFieldDelimiter(','); //separator
            file.inRecordDelimiter('\r\n');
        }

        //Read a CSV File
        container rec;
        ttsbegin;

        while(file.status() == IO_Status::Ok)
       //while (!file.status())
        {
            counter++;
            rec = file.read();
            if (conLen(rec))
            {
                if(header == true)
                {
                    header = false; //This CSV file will always have header
                    continue;
                    
                }
          //My code
           info("Import Completed");
            ttscommit;

Please guide me on this.

Thanks in advance,
Mark

You can easily enforce a particular file extension. You can create a class similar to XMLFileUploadTemporaryStorageStrategy and pass its instance to GetFileFromUser() as a parameter.

Determining whether file contents is valid CSV is much more tricky and I wouldn’t try to do it at all. It’s fine that the import won’t work if a user provides an invalid file.

1 Like

Thanks @MartinDrab.

That issue got resolved as per your comments. I have got another error that my code is behaving strange that, if I import CSV UTF-8 file its completely behaving strange and taking wrong records. Its working only on Coma delimited file.

Could you please guide me on this issue

Thanks in Advance,
Mark

Please create a new thread (with appropriate title and tags) and explain your problem in detail there.

It would be confusing if we discussed here, because it’s about something else and it’s already answered.