Problem uploading file with FormData in Angular 6










1















I'm trying to build a file upload system with Angular 6 for the UI and Lumen for the API.
The file upload works fine when I use the API directly with Postman.
But it's not working with the Angular UI.
I'm using FormData to handle the uploading but the API returns a null value for the file I'm trying to upload.



HTML:



<input type="file" name="document" (change)="onFileChanged($event.target.files)">


Angular code:



public onFileChanged(files: any): void 
var file = files[0];
var fd = new FormData();
fd.append('document', file);
this.documentService
.uploadDocument(this.id, fd)
.subscribe(
() =>
console.log('success');
,
() =>
console.log('failure');

);










share|improve this question


























    1















    I'm trying to build a file upload system with Angular 6 for the UI and Lumen for the API.
    The file upload works fine when I use the API directly with Postman.
    But it's not working with the Angular UI.
    I'm using FormData to handle the uploading but the API returns a null value for the file I'm trying to upload.



    HTML:



    <input type="file" name="document" (change)="onFileChanged($event.target.files)">


    Angular code:



    public onFileChanged(files: any): void 
    var file = files[0];
    var fd = new FormData();
    fd.append('document', file);
    this.documentService
    .uploadDocument(this.id, fd)
    .subscribe(
    () =>
    console.log('success');
    ,
    () =>
    console.log('failure');

    );










    share|improve this question
























      1












      1








      1


      1






      I'm trying to build a file upload system with Angular 6 for the UI and Lumen for the API.
      The file upload works fine when I use the API directly with Postman.
      But it's not working with the Angular UI.
      I'm using FormData to handle the uploading but the API returns a null value for the file I'm trying to upload.



      HTML:



      <input type="file" name="document" (change)="onFileChanged($event.target.files)">


      Angular code:



      public onFileChanged(files: any): void 
      var file = files[0];
      var fd = new FormData();
      fd.append('document', file);
      this.documentService
      .uploadDocument(this.id, fd)
      .subscribe(
      () =>
      console.log('success');
      ,
      () =>
      console.log('failure');

      );










      share|improve this question














      I'm trying to build a file upload system with Angular 6 for the UI and Lumen for the API.
      The file upload works fine when I use the API directly with Postman.
      But it's not working with the Angular UI.
      I'm using FormData to handle the uploading but the API returns a null value for the file I'm trying to upload.



      HTML:



      <input type="file" name="document" (change)="onFileChanged($event.target.files)">


      Angular code:



      public onFileChanged(files: any): void 
      var file = files[0];
      var fd = new FormData();
      fd.append('document', file);
      this.documentService
      .uploadDocument(this.id, fd)
      .subscribe(
      () =>
      console.log('success');
      ,
      () =>
      console.log('failure');

      );







      angular form-data






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 2:37









      ajgismeajgisme

      2011220




      2011220






















          2 Answers
          2






          active

          oldest

          votes


















          0














          I have used form data in my project. have a look in my code.it may help you



           uploadFile(data, index) 
          const formData: FormData = new FormData();
          console.log('formData', formData);

          formData.append('data', this.tableData.caseId);
          formData.append('file', data.filedata);
          this.dashService.uploadAttachment(formData, this.encodedData)
          .subscribe(
          (respData) =>
          this.filesArray.splice(index, 1);
          ,
          respError =>
          console.log('respError', respError);

          );

          removeFile(index)
          this.filesArray.splice(index, 1);



          The below is the service used in the code.



           uploadAttachment(data, encodedData) 
          return this._http.post(this._url + 'cases/attachment', data,
          headers:
          // 'Content-Type': 'multipart/form-data',
          'Accept': 'application/json',
          'Authorization': encodedData

          );



          I was facing problem for the content type i was using in the service. So i have commented it and my code worked fine.






          share|improve this answer























          • Thanks - tried that but didn't make a difference, there's not that much actually fundamentally different from our code tbh

            – ajgisme
            Nov 16 '18 at 11:36


















          0














          OK I found the problem!



          It was because I was using an interceptor that was adding JSON headers to every request so it was converting everything to JSON which obviously wouldn't work. So I just removed that header and it worked.






          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53330653%2fproblem-uploading-file-with-formdata-in-angular-6%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I have used form data in my project. have a look in my code.it may help you



             uploadFile(data, index) 
            const formData: FormData = new FormData();
            console.log('formData', formData);

            formData.append('data', this.tableData.caseId);
            formData.append('file', data.filedata);
            this.dashService.uploadAttachment(formData, this.encodedData)
            .subscribe(
            (respData) =>
            this.filesArray.splice(index, 1);
            ,
            respError =>
            console.log('respError', respError);

            );

            removeFile(index)
            this.filesArray.splice(index, 1);



            The below is the service used in the code.



             uploadAttachment(data, encodedData) 
            return this._http.post(this._url + 'cases/attachment', data,
            headers:
            // 'Content-Type': 'multipart/form-data',
            'Accept': 'application/json',
            'Authorization': encodedData

            );



            I was facing problem for the content type i was using in the service. So i have commented it and my code worked fine.






            share|improve this answer























            • Thanks - tried that but didn't make a difference, there's not that much actually fundamentally different from our code tbh

              – ajgisme
              Nov 16 '18 at 11:36















            0














            I have used form data in my project. have a look in my code.it may help you



             uploadFile(data, index) 
            const formData: FormData = new FormData();
            console.log('formData', formData);

            formData.append('data', this.tableData.caseId);
            formData.append('file', data.filedata);
            this.dashService.uploadAttachment(formData, this.encodedData)
            .subscribe(
            (respData) =>
            this.filesArray.splice(index, 1);
            ,
            respError =>
            console.log('respError', respError);

            );

            removeFile(index)
            this.filesArray.splice(index, 1);



            The below is the service used in the code.



             uploadAttachment(data, encodedData) 
            return this._http.post(this._url + 'cases/attachment', data,
            headers:
            // 'Content-Type': 'multipart/form-data',
            'Accept': 'application/json',
            'Authorization': encodedData

            );



            I was facing problem for the content type i was using in the service. So i have commented it and my code worked fine.






            share|improve this answer























            • Thanks - tried that but didn't make a difference, there's not that much actually fundamentally different from our code tbh

              – ajgisme
              Nov 16 '18 at 11:36













            0












            0








            0







            I have used form data in my project. have a look in my code.it may help you



             uploadFile(data, index) 
            const formData: FormData = new FormData();
            console.log('formData', formData);

            formData.append('data', this.tableData.caseId);
            formData.append('file', data.filedata);
            this.dashService.uploadAttachment(formData, this.encodedData)
            .subscribe(
            (respData) =>
            this.filesArray.splice(index, 1);
            ,
            respError =>
            console.log('respError', respError);

            );

            removeFile(index)
            this.filesArray.splice(index, 1);



            The below is the service used in the code.



             uploadAttachment(data, encodedData) 
            return this._http.post(this._url + 'cases/attachment', data,
            headers:
            // 'Content-Type': 'multipart/form-data',
            'Accept': 'application/json',
            'Authorization': encodedData

            );



            I was facing problem for the content type i was using in the service. So i have commented it and my code worked fine.






            share|improve this answer













            I have used form data in my project. have a look in my code.it may help you



             uploadFile(data, index) 
            const formData: FormData = new FormData();
            console.log('formData', formData);

            formData.append('data', this.tableData.caseId);
            formData.append('file', data.filedata);
            this.dashService.uploadAttachment(formData, this.encodedData)
            .subscribe(
            (respData) =>
            this.filesArray.splice(index, 1);
            ,
            respError =>
            console.log('respError', respError);

            );

            removeFile(index)
            this.filesArray.splice(index, 1);



            The below is the service used in the code.



             uploadAttachment(data, encodedData) 
            return this._http.post(this._url + 'cases/attachment', data,
            headers:
            // 'Content-Type': 'multipart/form-data',
            'Accept': 'application/json',
            'Authorization': encodedData

            );



            I was facing problem for the content type i was using in the service. So i have commented it and my code worked fine.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 16 '18 at 5:41









            AbhizAbhiz

            19014




            19014












            • Thanks - tried that but didn't make a difference, there's not that much actually fundamentally different from our code tbh

              – ajgisme
              Nov 16 '18 at 11:36

















            • Thanks - tried that but didn't make a difference, there's not that much actually fundamentally different from our code tbh

              – ajgisme
              Nov 16 '18 at 11:36
















            Thanks - tried that but didn't make a difference, there's not that much actually fundamentally different from our code tbh

            – ajgisme
            Nov 16 '18 at 11:36





            Thanks - tried that but didn't make a difference, there's not that much actually fundamentally different from our code tbh

            – ajgisme
            Nov 16 '18 at 11:36













            0














            OK I found the problem!



            It was because I was using an interceptor that was adding JSON headers to every request so it was converting everything to JSON which obviously wouldn't work. So I just removed that header and it worked.






            share|improve this answer



























              0














              OK I found the problem!



              It was because I was using an interceptor that was adding JSON headers to every request so it was converting everything to JSON which obviously wouldn't work. So I just removed that header and it worked.






              share|improve this answer

























                0












                0








                0







                OK I found the problem!



                It was because I was using an interceptor that was adding JSON headers to every request so it was converting everything to JSON which obviously wouldn't work. So I just removed that header and it worked.






                share|improve this answer













                OK I found the problem!



                It was because I was using an interceptor that was adding JSON headers to every request so it was converting everything to JSON which obviously wouldn't work. So I just removed that header and it worked.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 '18 at 11:10









                ajgismeajgisme

                2011220




                2011220



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53330653%2fproblem-uploading-file-with-formdata-in-angular-6%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    27

                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                    Category:Rhetoric