Problem sending mail with attachment using AngularJS












0















I am using Angular. When I send a mail with attachment to email, the response was that I received the data code of the file instead its actual file format.
like this:



enter image description here



I don't know what's the problem.



here's the code :
html :



<div class="uploadfile">
<input fileread="formData.attached" (change)="onFileSelected" type="file" id="real-file" hidden="hidden" />
<button type="button" id="custom-button">Upload CV</button>
<span id="custom-text">No file chosen, yet.</span>
</div>

<!--Script upload -->
<script>
const realFileBtn = document.getElementById("real-file");
const customBtn = document.getElementById("custom-button");
const customTxt = document.getElementById("custom-text");

customBtn.addEventListener("click", function() {
realFileBtn.click();
});

realFileBtn.addEventListener("change", function() {
if (realFileBtn.value) {
customTxt.innerHTML = realFileBtn.value.match(
/[/\]([wds.-()]+)$/
)[1];
} else {
customTxt.innerHTML = "No file chosen, yet.";
}
});
</script>
<!--End of Script upload -->


app.js :



myApp.directive("fileread", [function () {
return {
scope: {
fileread: "="
},
link: function (scope, element, attributes) {
element.bind("change", function (changeEvent) {
var reader = new FileReader();
reader.onload = function (loadEvent) {
scope.$apply(function () {
scope.fileread = loadEvent.target.result;
});
}
reader.readAsDataURL(changeEvent.target.files[0]);
});
}
}
}]);


Hope you can help.
Thanks in advance.










share|improve this question





























    0















    I am using Angular. When I send a mail with attachment to email, the response was that I received the data code of the file instead its actual file format.
    like this:



    enter image description here



    I don't know what's the problem.



    here's the code :
    html :



    <div class="uploadfile">
    <input fileread="formData.attached" (change)="onFileSelected" type="file" id="real-file" hidden="hidden" />
    <button type="button" id="custom-button">Upload CV</button>
    <span id="custom-text">No file chosen, yet.</span>
    </div>

    <!--Script upload -->
    <script>
    const realFileBtn = document.getElementById("real-file");
    const customBtn = document.getElementById("custom-button");
    const customTxt = document.getElementById("custom-text");

    customBtn.addEventListener("click", function() {
    realFileBtn.click();
    });

    realFileBtn.addEventListener("change", function() {
    if (realFileBtn.value) {
    customTxt.innerHTML = realFileBtn.value.match(
    /[/\]([wds.-()]+)$/
    )[1];
    } else {
    customTxt.innerHTML = "No file chosen, yet.";
    }
    });
    </script>
    <!--End of Script upload -->


    app.js :



    myApp.directive("fileread", [function () {
    return {
    scope: {
    fileread: "="
    },
    link: function (scope, element, attributes) {
    element.bind("change", function (changeEvent) {
    var reader = new FileReader();
    reader.onload = function (loadEvent) {
    scope.$apply(function () {
    scope.fileread = loadEvent.target.result;
    });
    }
    reader.readAsDataURL(changeEvent.target.files[0]);
    });
    }
    }
    }]);


    Hope you can help.
    Thanks in advance.










    share|improve this question



























      0












      0








      0








      I am using Angular. When I send a mail with attachment to email, the response was that I received the data code of the file instead its actual file format.
      like this:



      enter image description here



      I don't know what's the problem.



      here's the code :
      html :



      <div class="uploadfile">
      <input fileread="formData.attached" (change)="onFileSelected" type="file" id="real-file" hidden="hidden" />
      <button type="button" id="custom-button">Upload CV</button>
      <span id="custom-text">No file chosen, yet.</span>
      </div>

      <!--Script upload -->
      <script>
      const realFileBtn = document.getElementById("real-file");
      const customBtn = document.getElementById("custom-button");
      const customTxt = document.getElementById("custom-text");

      customBtn.addEventListener("click", function() {
      realFileBtn.click();
      });

      realFileBtn.addEventListener("change", function() {
      if (realFileBtn.value) {
      customTxt.innerHTML = realFileBtn.value.match(
      /[/\]([wds.-()]+)$/
      )[1];
      } else {
      customTxt.innerHTML = "No file chosen, yet.";
      }
      });
      </script>
      <!--End of Script upload -->


      app.js :



      myApp.directive("fileread", [function () {
      return {
      scope: {
      fileread: "="
      },
      link: function (scope, element, attributes) {
      element.bind("change", function (changeEvent) {
      var reader = new FileReader();
      reader.onload = function (loadEvent) {
      scope.$apply(function () {
      scope.fileread = loadEvent.target.result;
      });
      }
      reader.readAsDataURL(changeEvent.target.files[0]);
      });
      }
      }
      }]);


      Hope you can help.
      Thanks in advance.










      share|improve this question
















      I am using Angular. When I send a mail with attachment to email, the response was that I received the data code of the file instead its actual file format.
      like this:



      enter image description here



      I don't know what's the problem.



      here's the code :
      html :



      <div class="uploadfile">
      <input fileread="formData.attached" (change)="onFileSelected" type="file" id="real-file" hidden="hidden" />
      <button type="button" id="custom-button">Upload CV</button>
      <span id="custom-text">No file chosen, yet.</span>
      </div>

      <!--Script upload -->
      <script>
      const realFileBtn = document.getElementById("real-file");
      const customBtn = document.getElementById("custom-button");
      const customTxt = document.getElementById("custom-text");

      customBtn.addEventListener("click", function() {
      realFileBtn.click();
      });

      realFileBtn.addEventListener("change", function() {
      if (realFileBtn.value) {
      customTxt.innerHTML = realFileBtn.value.match(
      /[/\]([wds.-()]+)$/
      )[1];
      } else {
      customTxt.innerHTML = "No file chosen, yet.";
      }
      });
      </script>
      <!--End of Script upload -->


      app.js :



      myApp.directive("fileread", [function () {
      return {
      scope: {
      fileread: "="
      },
      link: function (scope, element, attributes) {
      element.bind("change", function (changeEvent) {
      var reader = new FileReader();
      reader.onload = function (loadEvent) {
      scope.$apply(function () {
      scope.fileread = loadEvent.target.result;
      });
      }
      reader.readAsDataURL(changeEvent.target.files[0]);
      });
      }
      }
      }]);


      Hope you can help.
      Thanks in advance.







      javascript html css angular






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 3 at 12:21









      wanttobeprofessional

      1,03131323




      1,03131323










      asked Jan 2 at 3:02









      d.unknownd.unknown

      14




      14
























          1 Answer
          1






          active

          oldest

          votes


















          0














          In the image you attached, I saw that the file is actually 64 based string. So the problem is from your Angular Code.



          reader.readAsDataURL(changeEvent.target.files[0]);


          readAsDataURL will return the base64 encoded string. You can check the link here FileReader.readAsDataURL()



          As the result, you just receive the string in your email, but not actual file. So you should specify file name and encoding = base64.



          I'm not sure that technology in the back end that you are using. You can check the sample nodeJs code here.






          const mailOptions = {
          ...
          attachments: [
          { // encoded string as an attachment
          filename: yourFileName,
          content: fileString,
          encoding: 'base64'
          }
          ]
          };








          share|improve this answer
























          • Hi, thank you so much for answering, I'm still new in coding maybe you can also check my backend code here : $scope.formData = { type:'careers', name : '', contact : '', email : '', position : '', attached : '', }; I still don't make it work :(, Thank you so much!

            – d.unknown
            Jan 8 at 9:50













          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%2f54000765%2fproblem-sending-mail-with-attachment-using-angularjs%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          In the image you attached, I saw that the file is actually 64 based string. So the problem is from your Angular Code.



          reader.readAsDataURL(changeEvent.target.files[0]);


          readAsDataURL will return the base64 encoded string. You can check the link here FileReader.readAsDataURL()



          As the result, you just receive the string in your email, but not actual file. So you should specify file name and encoding = base64.



          I'm not sure that technology in the back end that you are using. You can check the sample nodeJs code here.






          const mailOptions = {
          ...
          attachments: [
          { // encoded string as an attachment
          filename: yourFileName,
          content: fileString,
          encoding: 'base64'
          }
          ]
          };








          share|improve this answer
























          • Hi, thank you so much for answering, I'm still new in coding maybe you can also check my backend code here : $scope.formData = { type:'careers', name : '', contact : '', email : '', position : '', attached : '', }; I still don't make it work :(, Thank you so much!

            – d.unknown
            Jan 8 at 9:50


















          0














          In the image you attached, I saw that the file is actually 64 based string. So the problem is from your Angular Code.



          reader.readAsDataURL(changeEvent.target.files[0]);


          readAsDataURL will return the base64 encoded string. You can check the link here FileReader.readAsDataURL()



          As the result, you just receive the string in your email, but not actual file. So you should specify file name and encoding = base64.



          I'm not sure that technology in the back end that you are using. You can check the sample nodeJs code here.






          const mailOptions = {
          ...
          attachments: [
          { // encoded string as an attachment
          filename: yourFileName,
          content: fileString,
          encoding: 'base64'
          }
          ]
          };








          share|improve this answer
























          • Hi, thank you so much for answering, I'm still new in coding maybe you can also check my backend code here : $scope.formData = { type:'careers', name : '', contact : '', email : '', position : '', attached : '', }; I still don't make it work :(, Thank you so much!

            – d.unknown
            Jan 8 at 9:50
















          0












          0








          0







          In the image you attached, I saw that the file is actually 64 based string. So the problem is from your Angular Code.



          reader.readAsDataURL(changeEvent.target.files[0]);


          readAsDataURL will return the base64 encoded string. You can check the link here FileReader.readAsDataURL()



          As the result, you just receive the string in your email, but not actual file. So you should specify file name and encoding = base64.



          I'm not sure that technology in the back end that you are using. You can check the sample nodeJs code here.






          const mailOptions = {
          ...
          attachments: [
          { // encoded string as an attachment
          filename: yourFileName,
          content: fileString,
          encoding: 'base64'
          }
          ]
          };








          share|improve this answer













          In the image you attached, I saw that the file is actually 64 based string. So the problem is from your Angular Code.



          reader.readAsDataURL(changeEvent.target.files[0]);


          readAsDataURL will return the base64 encoded string. You can check the link here FileReader.readAsDataURL()



          As the result, you just receive the string in your email, but not actual file. So you should specify file name and encoding = base64.



          I'm not sure that technology in the back end that you are using. You can check the sample nodeJs code here.






          const mailOptions = {
          ...
          attachments: [
          { // encoded string as an attachment
          filename: yourFileName,
          content: fileString,
          encoding: 'base64'
          }
          ]
          };








          const mailOptions = {
          ...
          attachments: [
          { // encoded string as an attachment
          filename: yourFileName,
          content: fileString,
          encoding: 'base64'
          }
          ]
          };





          const mailOptions = {
          ...
          attachments: [
          { // encoded string as an attachment
          filename: yourFileName,
          content: fileString,
          encoding: 'base64'
          }
          ]
          };






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 8:54









          Nguyen Manh TungNguyen Manh Tung

          6




          6













          • Hi, thank you so much for answering, I'm still new in coding maybe you can also check my backend code here : $scope.formData = { type:'careers', name : '', contact : '', email : '', position : '', attached : '', }; I still don't make it work :(, Thank you so much!

            – d.unknown
            Jan 8 at 9:50





















          • Hi, thank you so much for answering, I'm still new in coding maybe you can also check my backend code here : $scope.formData = { type:'careers', name : '', contact : '', email : '', position : '', attached : '', }; I still don't make it work :(, Thank you so much!

            – d.unknown
            Jan 8 at 9:50



















          Hi, thank you so much for answering, I'm still new in coding maybe you can also check my backend code here : $scope.formData = { type:'careers', name : '', contact : '', email : '', position : '', attached : '', }; I still don't make it work :(, Thank you so much!

          – d.unknown
          Jan 8 at 9:50







          Hi, thank you so much for answering, I'm still new in coding maybe you can also check my backend code here : $scope.formData = { type:'careers', name : '', contact : '', email : '', position : '', attached : '', }; I still don't make it work :(, Thank you so much!

          – d.unknown
          Jan 8 at 9:50






















          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%2f54000765%2fproblem-sending-mail-with-attachment-using-angularjs%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

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas

          Can't read property showImagePicker of undefined in react native iOS