注意:我已经使用上面的代码上传了 CSV 文件并读取并验证了数据,但我无法将这些记录保存到数据库中。任何人都可以帮助我将数据从我的 service.ts 传递到 webpapi 控制器的操作方法。
波纹管代码是用于 csv 文件上传的 html 代码 -
<td><input type="file" name="File Upload" id="txtFileUpload"
(change)="changeListener($event.target.files)"
accept=".csv"/></td>
<td> <button class="btn btn1 btn2" color="primary" mat-raised-button *ngIf="isUploadFile" (click)="uploadCSVFile($event)">
UploadFile
</button></td>
Bellow code is Component.ts code -
uploadCSVFile($event) {
event.preventDefault();
alert('upload');
let businessAccountData = this.csvRecords;
let merchantdata: UserMerchantData = this.commonfunction.getUserMerchantData();
let email = this.commonfunction.getEmail();
// let userName = this.commonfunction.getUserName();
// for(let i =1; i<= businessAccountData.length;i++)
// {
// businessAccountData[i].UserName =userName;
// businessAccountData[i].MerchantKeyId =merchantdata.MerchantKeyId;
// }
this.service.addOnBusinessAccountData(businessAccountData,email).subscribe(result => {
console.log(' result - ', result);
if (result != null) {
//this.router.navigate(['/home/addonBusiness/manage-business-accounts']);
}
});
}
below code is service.ts file -
**strong text**
addOnBusinessAccountData(data,email)
{
alert('service');
// alert(JSON.stringify(data));
alert(data);
return this.service.create(environment.createBusinessAccountDataUrl+"?email="+email, data);
}
below code is for webapi controller's action method -
[HttpPost]
public IHttpActionResult AddAccountData(AddOnAccountData[] addOnAccountData, string email)
{
try
{
_apiConnectivity = new VposApiConnnectivity(email);
var businessAccountResponse = _apiConnectivity.AddAccountData(addOnAccountData);
return Json(businessAccountResponse);
}
catch (Exception e)
{
return Ok("");
}
}