0

here grabs the image of the form, and I call the service to pass the data

handleFileInput(file: FileList) {
    this.fileToUpload = file.item(0);
  }
  
  CreateDetailProduct(){
    let form = this.FormDetailProductCreate.value;
    let detailproduct = new Detailproduct(); 

    detailproduct.Id_Product = form.Id_Product.id;
    detailproduct.Id_TypeProduct = form.Id_TypeProduct.id;
    detailproduct.Id_Provider = form.Id_Provider.id;
    detailproduct.TotalPrice = form.TotalPrice;
    detailproduct.Quantity = form.Quantity;
    
    detailproduct.Image = this.fileToUpload;


      this.servicedetailproduct.postDetailProduct(detailproduct).subscribe();

 
  }
 <div class="button-row"> 
        <button matSuffix mat-mini-fab color="primary" (click)="imgFileInput.click()">
          <mat-icon>attachment</mat-icon>
        </button> 
        <input  formControlName="Image" hidden type="file" #imgFileInput accept="image/*" (change)="handleFileInput($event.target.files)"/> 
        <img class="imagesize" [src]="imagesUrl" alt="">
      </div>
      
      

from django.db import models
from Product.models import ProductModel
from TipoProducto.models import TipoProductoModel
from Proveedor.models import ProveedorModel
from LoteProducto.models import LoteProductoModel


class ProductDetailModel(models.Model):
    Id_Product = models.ForeignKey(ProductModel, null =False, blank=False, on_delete=models.CASCADE)
    Id_TypeProduct = models.ForeignKey(TipoProductoModel, null =False, blank=False, on_delete=models.CASCADE)
    Id_Lote = models.ForeignKey(LoteProductoModel, null =False, blank=False, on_delete=models.CASCADE)
    Id_Provider = models.ForeignKey(ProveedorModel, null =False, blank=False, on_delete=models.CASCADE)
    TotalPrice = models.FloatField()

    Quantity = models.IntegerField()
    Image = models.ImageField(upload_to="ProductDetail/images/", null=True, blank=True)

I get this error when you sent the image

{Image: ["The submitted data was not a file. Check the encoding type on the form."]}

how can I do to load the image in angular 5 to django, or what am I doing wrong

1 Answer 1

1

in your post method in angular you have to do something like this:

postDetailProduct(detailproduct) {
    let formData: FormData = new FormData();

    if (detailproduct) {
        for(let key in detailproduct){
            formData.append(key, detailproduct[key]);
        }
    }

    formData.append('thumbnail', detailproduct.Image, detailproduct.Image.name);

    return this._http.post(url, formData)
        .pipe(
            map(res => res.json())
        );
 }

Pd: let me know if this solution works for you.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.