我尝试将图像和输入文本字段上传到ASP Net Core Web API,但控制器未读取(Angular 13,Net 5)

发布于 2025-02-04 18:13:19 字数 3195 浏览 2 评论 0原文

我需要将产品详细信息和产品图像上传到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}");
        }
        
    }

Folder Structure
enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文