标签:style family rtp mat bottom public ext 读取文件 add
Dynamic 365开发中对于读取CSV文件与2012略有不同。Dynamic 365中,对于文件的处理是先上传,后下载的过程。需要通过FileUpload control 和Upload strategy class ,FileUploadTemporaryStorageStrategy类来实现对于文件的读取和下载。
以下是一个简单的例子可供参考:
Dilaog窗体,读取文件上传到本地服务器中,以URL方式可以查看
Public Object dialog()
{
DialogGroup dialogGroup;
FormBuildControl formBuildControl;
FileUploadBuild dialogFileUpload;
#file
;
dialog = super();
dialogGroup = dialog.addGroup("File path");
formBuildControl = dialog.formBuildDesign().control(dialogGroup.name());
dialogFileUpload = formBuildControl.addControlEx(classstr(FileUpload), "dialogFileUpload");
dialogFileUpload.style(FileUploadStyle::MinimalWithFilename);
dialogFileUpload.baseFileUploadStrategyClassName(classstr(FileUploadTemporaryStorageStrategy));
dialogFileUpload.fileTypesAccepted(".csv");
dialogFileUpload.fileNameLabel("Select a file to upload");
return dialog;
}
得到控件名:
protected FormControl getFormControl(DialogRunbase _dialog, str _controlName)
{
return _dialog.formRun().control(_dialog.formRun().controlId( _controlName));
}
获取文件:
public boolean getFromDialog(
{
boolean ret;
;
ret = super();
FileUpload fileupload;
fileupload = this.getFormControl(dialog, "dialogFileUpload");
fileName = fileupload.fileName();
if (fileName == ‘‘)
{
checkFailed("File name empty");
}
return ret;
}
读取数据:
private void readData()
{
int startpos;
int fieldCSVPos;
container conData ;
SysDictTable sysDictTable;
container currentLine;
int totalOfLines;
CommaTextStreamIo localStream;
FileUpload fileUploadControl = this.getFormControl(dialog, “dialogFileUpload”);
FileUploadTemporaryStorageResult fileUploadResult = fileUploadControl.getFileUploadResult();
if (fileUploadResult != null && fileUploadResult.getUploadStatus())
{
fileName = fileUploadResult.getDownloadUrl();
}
localStream = CommaTextStreamIo::constructForRead(File::UseFileFromURL(fileName));
if (localStream.status() != IO_Status::Ok)
{
throw error(strfmt(‘Is not possible to open the file. Error %1‘,enum2str(localStream.status())));
}
localStream.inFieldDelimiter("\,");
localStream.inRecordDelimiter("\n");
currentLine = localStream.read();
startpos = 1;//定义读取数据的开始行标
ttsbegin;
while (currentLine)
{
if ( currentLine == connull() ) continue ;
row++ ;
if ( row < startpos ) continue ;
this.writeData(currentLine) ;
// info(strFmt(‘%1‘,conPeek(currentLine,2)));
currentLine = localStream.read();
}
ttscommit;
}
欢迎大家讨论,一起学习。
想了解更多,可参考 https://community.dynamics.com/ax/b/365operationswithsukrut/archive/2017/09/28/d365fo-use-of-fileupload-control-to-read-csv-file
标签:style family rtp mat bottom public ext 读取文件 add
原文地址:https://www.cnblogs.com/sunny-technology/p/9280737.html