File dialog open for read file data in Ax 2012

Description:-

In this article we will see about how to open file dialog for read csv file from the path and get csv file data.

static void FileDialogOpen_ReadFileData(Args _args)
{
    Dialog                              dialog  = new Dialog();
    str                                 filePath,fileNameOnly;
    filetype                            type;
    AsciiIo                             importFile,logFile;
    DialogField                         dialogFileName,dialogField;
    SysOperationProgress                simpleProgress;
    Filename                            filename;
    AsciiIo                             csvFile;
    container                           readCon;
    Container                           filterCriteria;
    FileIOPermission                    permission;
    CommaIo                             file;
    textBuffer                          tb = new textBuffer();
    #File
    #avifiles
    ;
    dialog = new Dialog('Select CSV File');
    dialogFileName = dialog.addField("Filenameopen", "File Name");
    filterCriteria = ['csv','*.csv'];
    filterCriteria = dialog.filenameLookupFilter(filterCriteria);
    dialog.run();
    if (dialog.run())
    {
        filename = dialogFileName.value();
    }
    if(!filename)
    {
        info("Filename must be filled");
    }
    permission = new fileIOpermission(filename,#io_read);
    permission.assert();
    file = new CommaIo(filename,#io_read);
    if (!file)
    throw error("Error reading file");
    tb.fromFile(filename); //File name with Path …
    csvFile = new CommaIo(filename, 'r');
    simpleProgress = SysOperationProgress::newGeneral(#aviUpdate, 'Importing data',1000);
    try
    {
        if (csvFile) //Checking for csv file.
        {
            readCon = csvFile.read();//reading the file.

            while(csvFile.status() ==  IO_Status::Ok)
            {
                readCon = csvFile.read();//reading the file.
                if(!readCon)
                break;
                info(strFmt("%1, %2",conPeek(readCon,1),conPeek(readCon,2)));
            }
        }
    }
    catch
    {
        throw error("");
    }
}

Related Posts

Previous
Next Post »

Thanks for comments.....