0

I'm trying to upload an image using mevdschee's PHP-CRUD-API. I've tried almost everything.. theese are the service functions I've tried, using both FormData or simple json.

service.ts

addMapIcon(mapIcon:mapIcon): Observable<string>{
    let fd = new FormData;
    fd.append('active', mapIcon.active?'1':'0');
    fd.append('file',mapIcon.file);
    fd.append('name',mapIcon.name);
    return this.http.post<string>(this.iconApiUrl, fd);

..or even:

    return this.http.post<string>(this.iconApiUrl, fd, {
        headers: new HttpHeaders({
          'Content-Type': 'application/json',
          'Content-type':'multipart/form-data'
          + combination of others parameters
          }));
    
  }
addMapIcon(mapIcon:mapIcon): Observable<string>{
      return this.http.post<string>(this.iconApiUrl, mapIcon, {
        headers: new HttpHeaders({
          'Content-Type': 'application/json',
          }),
        });  
      }

component.ts

onIconChange(e) {
    const reader = new FileReader();
    
    if(e.target.files && e.target.files.length) {
      const [file] = e.target.files;
      reader.readAsDataURL(file);
    
      reader.onload = () => {
        this.mapIcon.file = file; //(or even reader.result as string; as found in some example)
        this.mapIcon.name = file.name;
        this.mapIcon.active = true;
        //this.icon.file = file;
        /*this.uploadForm.patchValue({
          imgSrc: reader.result
        });*/
   
      };
    }
  }

the file field is defined as mediumblob in mysql.

The error I get is "Data integrity violation".

Can't understand where the error is..

3
  • If you're going to post an error message, post all of it. There appears to be a problem with your MySQL schema but you've only posted part of the error message and none of the relevant code or table structure. Commented Sep 19, 2021 at 1:45
  • I'm using mevdschee's treeql PDO PHP-CRUD-API, the only message I get is 1010 - "Data integrity violation". Commented Sep 19, 2021 at 2:54
  • treeql.org/learn, search 1010, is a conflit. I'm trying to post via swagger but again get the error. I only can post if I put a little string in the file field (via swagger) Commented Sep 19, 2021 at 2:56

2 Answers 2

2

The "Data integrity violation" error is caused by a failing constraint. This may be:

  • a foreign key constraint that is failing (referenced record does not exist)
  • a missing field value (on a field without a default value)
  • a null value where it is not allowed (the field is not nullable)

You may enable debug mode ('debug' => true in the config) and look at the X-Exception-Message HTTP header of the response (using the Network tab of the Developer tools of your browser). It will contain the cause of the problem.

NB: I am "mevdschee", the author of the PHP-CRUD-API.

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

2 Comments

I've looked around how to enable debug in the docs.. but haven't found..my bad. thank u so much
@NikRubblers Please accept the answer if it helped you solve the problem.
2

are you able to send image blob successfully to backend using post request? If not try this :

addMapIcon(mapIcon:mapIcon): Observable<string>{
    let fd = new FormData();
    fd.append('active', mapIcon.active?'1':'0');
    **fd.append('file',mapIcon.file, mapIcon.name);**
    fd.append('name',mapIcon.name);

    return this.http.post<string>(this.iconApiUrl, fd, {
        headers: new HttpHeaders({
          'Content-type':'multipart/form-data'
          }));
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.