我是JHIPSTER的新手,并尝试创建一个基本的应用程序,该应用程序将允许用户上传CSV文件,然后在实体中查看内容。
我正在尝试找出一种用用户上传的CSV BLOB中包含的数据填充第二个实体的方法。我创建了一个实体,该实体允许用户上传CSV文件并将其作为斑点存储在数据库中,并且我还创建了一项服务,目的是根据CSV文件的内容填充第二个实体,并使用记录填充第二个实体。已上传。
我将如何解决这个问题?我过去曾使用OpenCSV来读取CSV文件并通过其FilePath填充MySQL表,但是我不熟悉访问数据库中存储在数据库中的CSV文件。
我使用superCSV实现了相同的用例任何。这有点辱骂,但效果很好,创建另一个的实体只是记录上传的一种方式,这样我就可以重复使用Jhipster生成的UI而不会进行任何更改。
1.in html:
</td>
<td style="padding: 5px;">
<input type="button"
name="Reset"
id="txtFileReset"
class="btn btn-primary"
(click)="csvReset()"
value="Reset"/>
<input type="button"
name="Reset"
id="txtFileSave"
class="btn btn-primary"
(click)="csvSave()"
value="Save CSV To DB"/>
</td>
2.in TS:
csvRecords = [];
fileChangeListener($event): void {
const text = [];
const target = $event.target || $event.srcElement;
const files = target.files;
if (Constants.validateHeaderAndRecordLengthFlag) {
if (!this.fileUtil.isCSVFile(files[0])) {
alert('Please import valid .csv file.');
this.csvReset();
}
}
const input = $event.target;
const reader = new FileReader();
reader.readAsText(input.files[0]);
reader.onload = data => {
const csvData = reader.result;
const csvRecordsArray = csvData.split(/rn|n/);
let headerLength = -1;
if (Constants.isHeaderPresentFlag) {
const headersRow = this.fileUtil.getHeaderArray(csvRecordsArray, Constants.tokenDelimeter);
headerLength = headersRow.length;
}
this.csvRecords = this.fileUtil.getDataRecordsArrayFromCSVFile(
csvRecordsArray,
headerLength,
Constants.validateHeaderAndRecordLengthFlag,
Constants.tokenDelimeter
);
if (this.csvRecords === null) {
// If control reached here it means csv file contains error, reset file.
this.csvReset();
}
};
reader.onerror = function() {
alert('Unable to read ' + input.files[0]);
};
}
csvReset() {
this.elementRef.nativeElement.querySelector('#txtFileUpload').value = '';
this.csvRecords = [];
}
csvSave() {
this.ipInfo = new IpInfoSdmSuffix();
for (let i = 1; i < this.csvRecords.length; i++) {
this.ipInfo.name = this.csvRecords[i][0];
this.ipInfo.addressStart = this.csvRecords[i][1];
this.ipInfo.addressEnd = this.csvRecords[i][2];
this.ipInfo.validType = this.csvRecords[i][3];
this.subscribeToSaveResponse(this.ipInfoService.create(this.ipInfo));
}
}
private subscribeToSaveResponse(result: Observable<HttpResponse<IIpInfoSdmSuffix>>) {
result.subscribe((res: HttpResponse<IIpInfoSdmSuffix>) => this.onSaveSuccess(), (res: HttpErrorResponse) => this.onSaveError());
}
private onSaveSuccess() {
this.isSaving = false;
this.isCsvSaved = false;
// this.previousState();
this.clear();
}
private onSaveError() {
this.isSaving = false;
this.isCsvSaved = false;
}