I Try to send object and file at the same time to .net api but it dont work.
I am trying to send data with FormData on angular side but FormData does not accept objects. When I don't send it with FormData, I can't receive the Files on the .net side.
How can I send the files and the object to the .net side at the same time?
angular :
const formData = new FormData();
formData.append('type',this.form.get('type').value)
formData.append('productId',this.form.get('product').value);
formData.append('serialNo',this.form.get('serialNo').value);
formData.append('companyId',this.form.get('company').value);
formData.append('unitPrice',this.form.get('unitPrice').value.toString().replace('.',','));
formData.append('description',this.form.get('description').value);
formData.append('properties',this._helperService.mapToObj(this.properties))
const files = this.form.get('files').value as FileList;
const fileArr = Array.from(files);
if(fileArr.length > 0){
fileArr.forEach(f => formData.append('files',f))
}
this._stockReceiptService.saveStockReceipt(formData).pipe(takeUntil(this.subject)).subscribe(resp => {
this.success.text = "Stok girişi kaydedildi";
this.success.fire();
console.log("Apiden gelen => ", resp);
}, err => {
this.error.text = "Stok girişi yapılırken hata oluştu";
this.error.fire();
});
}
SaveStockReceipt Service:
saveStockReceipt(post: any): Observable<any>{
return this._http.post<any>(this.apiURL + '/stock/stockReceipt',post);
}
.net model
public class CreateStockVModel
{
[Required(ErrorMessage = "Type alanı zorunlu alandır.")]
public int Type { get; set; }
[Required(ErrorMessage = "ProductId alanı zorunlu alandır.")]
public int ProductId { get; set; }
[Required(ErrorMessage = "SerialNo alanı zorunlu alandır.")]
public string? SerialNo { get; set; }
[Required(ErrorMessage = "CompanyId alanı zorunlu alandır.")]
public int CompanyId { get; set; }
public decimal? UnitPrice { get; set; }
public string? Description { get; set; }
public List<IFormFile>? Files { get; set; }
public Dictionary<int,int>? Properties { get; set; }
}
controller
[HttpPost]
public async Task<IActionResult> StockReceipt([FromForm] CreateStockVModel vModel)
{
return Ok(vModel);
}