我尝试将图像和输入文本字段上传到ASP Net Core Web API,但控制器未读取(Angular 13,Net 5)
我需要将产品详细信息和产品图像上传到ASP.NET Core Web API中。我在下面实现HTML文件,
<form [formGroup]="myForm" (ngSubmit)="submit()">
<mat-card>
<h2 class="fw-bold text-center">Product Management</h2>
<label class="fw-bold" for="">Product Name</label>
<input type="text" name="name" id="name" formControlName="ProductName" class="form-control">
<input type="text" name="description" id="description" placeholder="Product Description" class="form-control mt-3 mb-2" formControlName="ProductDescription">
<input type="text" name="price" id="price" placeholder="Product Price" class="form-control mt-3 mb-2" formControlName="price">
<input type="text" name="created" id="created" placeholder="Product created" class="form-control mt-3 mb-2" formControlName="created">
<input type="text" name="cat" id="cat" placeholder="Product created" class="form-control mt-3 mb-2" formControlName="ProductCatID">
<input type="file" name="Image" id="Image" class="form-control mt-3 mb-2" (change)="onFileChange($event)" formControlName="ImageUrl">
<img [src]="imageSrc" *ngIf="imageSrc" style="height: 300px; width:500px">
<button type="submit" class="btn btn-primary btn-block mt-3">Submit</button>
</mat-card>
</form>
这是我已为提交数据实现的TS文件
submit(){
console.log(this.myForm.value);
this.http.post('https://localhost:5001/api/Products', this.myForm.value)
.subscribe(res => {
console.log(res);
alert('Uploaded Successfully.');
})
}
我已将控制器作为Bellow实现。该控制器未读取
[HttpPost]
public async Task<ActionResult<ProductDto>> CreateProductAsync(CreateProductDto pro, IFormFile Image)
{
try
{
if (Image == null || Image.Length == 0)
{
return Content("File not selected");
}
var path = Path.Combine(_environment.WebRootPath, "wwwroot//images", Image.FileName);
//Saving the image in that folder
using (FileStream stream = new FileStream(path, FileMode.Create))
{
await Image.CopyToAsync(stream);
stream.Close();
}
pro.ImageUrl = Image.FileName;
var productEntity = _mapper.Map<Product>(pro);
var newProduct = _SqlService.AddProduct(productEntity);
var productForReturn = _mapper.Map<ProductDto>(newProduct);
return CreatedAtRoute("GetProduct", new { id = productForReturn.ProId },
productForReturn);
}
catch(Exception ex)
{
return StatusCode(500, $"Internal server error: {ex}");
}
}
我需要将图像上传到Web API文件夹中,还需要将图像和产品详细信息存储在SQL数据库中
I need to upload product details and product images into ASP.NET core web API. I implement HTML files as below
<form [formGroup]="myForm" (ngSubmit)="submit()">
<mat-card>
<h2 class="fw-bold text-center">Product Management</h2>
<label class="fw-bold" for="">Product Name</label>
<input type="text" name="name" id="name" formControlName="ProductName" class="form-control">
<input type="text" name="description" id="description" placeholder="Product Description" class="form-control mt-3 mb-2" formControlName="ProductDescription">
<input type="text" name="price" id="price" placeholder="Product Price" class="form-control mt-3 mb-2" formControlName="price">
<input type="text" name="created" id="created" placeholder="Product created" class="form-control mt-3 mb-2" formControlName="created">
<input type="text" name="cat" id="cat" placeholder="Product created" class="form-control mt-3 mb-2" formControlName="ProductCatID">
<input type="file" name="Image" id="Image" class="form-control mt-3 mb-2" (change)="onFileChange($event)" formControlName="ImageUrl">
<img [src]="imageSrc" *ngIf="imageSrc" style="height: 300px; width:500px">
<button type="submit" class="btn btn-primary btn-block mt-3">Submit</button>
</mat-card>
</form>
This is TS file i have implement for the submit data
submit(){
console.log(this.myForm.value);
this.http.post('https://localhost:5001/api/Products', this.myForm.value)
.subscribe(res => {
console.log(res);
alert('Uploaded Successfully.');
})
}
I have implemented Controller As bellow. this controller not read
[HttpPost]
public async Task<ActionResult<ProductDto>> CreateProductAsync(CreateProductDto pro, IFormFile Image)
{
try
{
if (Image == null || Image.Length == 0)
{
return Content("File not selected");
}
var path = Path.Combine(_environment.WebRootPath, "wwwroot//images", Image.FileName);
//Saving the image in that folder
using (FileStream stream = new FileStream(path, FileMode.Create))
{
await Image.CopyToAsync(stream);
stream.Close();
}
pro.ImageUrl = Image.FileName;
var productEntity = _mapper.Map<Product>(pro);
var newProduct = _SqlService.AddProduct(productEntity);
var productForReturn = _mapper.Map<ProductDto>(newProduct);
return CreatedAtRoute("GetProduct", new { id = productForReturn.ProId },
productForReturn);
}
catch(Exception ex)
{
return StatusCode(500, quot;Internal server error: {ex}");
}
}
I need to upload images into the web API folder and as well as I need store the image and product details in SQL Database
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论