Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proper use of Container Model SDK #650

Open
2 of 6 tasks
al041265 opened this issue Oct 8, 2019 · 2 comments
Open
2 of 6 tasks

Proper use of Container Model SDK #650

al041265 opened this issue Oct 8, 2019 · 2 comments

Comments

@al041265
Copy link

al041265 commented Oct 8, 2019

What type of issue are you creating?

  • Bug
  • Enhancement
  • Question

What version of this module are you using?

  • 2.0.10 (Stable)
  • 2.1.0-rc.n (2.1 Release Candidate n)
  • Other

Write other if any:

So I've been searching any clue on how to properly use the container model generated for the sdk, I'm using angular 8 to try to upload to my loopback 3 server;

` onFileSelect(event) {
if (event.target.files.length > 0) {
const file = event.target.files[0].name;
const formData = new FormData();
var reader = new FileReader();
formData.append('file ',event.target.files[0]);
// console.log(formData)
for (var [key, value] of formData.entries()) {
console.log(key, value);
}

    this.containerapi.upload('docs',formData,(response)=>
    {
     // console.log(response)
    }).subscribe((d)=>
    {
      console.log(d)
    },(error)=>{
      console.log(error)
    })

 

}

}`
this is my component.ts

`




                    <input #file id="file-input" type="file"  (change)="onFileSelect($event)"/>
                </div>`

this si the html part where the user selects the file and then its supposed to upload it.
I can understand the first parameter on the upload method, but what exactly I'm supposed to put on the request parameter?
I made it work testing the server endpoint with insomnia, as a multipart form and having a parameter called 'file', selecting the file, submitting and it uploaded it perfectly, but now trying with the sdk I cant figure out what Im doing wrong, any idea?

@jgwinner
Copy link

Did you get any further?

I haven't tried uploading files, but may be doing this shortly.

If I do, I'll try to come back here and update it.

    == John ==

@al041265
Copy link
Author

Hi, sorry for the wait, at the end I couldnt use the integrated methods for upload, so I ended up using native post request to the backend endpoint.

` let formData: FormData = new FormData();
formData.append('file', this.file, this.file.name);
let headerst = new HttpHeaders();
headerst.append('Content-Type', 'multipart/form-data');
headerst.append('Accept', 'application/json');
var i: number = this.caso.idCaso;
var url = 'https://myapiurl.com/api/containers/' + i + '/upload'
this.hidden = false;
this.http.post(url, formData, { headers: headerst, reportProgress: true, observe: 'events' }).pipe(map((event: any) => {
switch (event.type) {

    case HttpEventType.UploadProgress:
      const progress = Math.round(100 * event.loaded / event.total);
      this.value = progress;
      console.log(progress)
      return { status: 'progress', message: progress };

    case HttpEventType.Response:

      console.log("FINISHED")
      return event.body;
    default:
      console.log("unhandled")
      return `Unhandled event: ${event.type}`;
  }
}))`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants