2

Im trying to Send an E-mail with an attachment, which the user can select with the <input type=file> HTML-Tag.

My idea was to read the File out of the input with the Filereader and get the Content and the Filename + type. With this three information i can create a new File with the Filename + type and the content. My First Question is:

Is this Method fine or is there a easier way to do it?

To My Problem/second Question:

This is my Code and everything works except writing the Content in the created File (PHP Code line 3). It creates the File to the right Path but the file is always empty, but selectedfile[0] is not empty. i tried with (.txt, .docx, .pdf)

This is the value of selectedfile[0] if i select a random .pdf file: enter image description here

This is my HTML:

<input class="swal2-file" type="file" id=f_file>

This is my JavaScript:

 async function() {
                    return new Promise((resolve, reject) => {
                        var selectedfile = null;

                        var email = $('#email').val();
                        var header = $('#header').val();
                        var content = $('#content').val();

                        var file =  $('#files')[0].files[0];
                        
                        if (file) {
                            var reader = new FileReader();
                            reader.readAsText(file, "UTF-8");
                            reader.onload = function (evt) {
                                resolve({
                                    'email': email,
                                    'header': header,
                                    'content': content,
                                    'selectedfile': [evt.target.result, file.name]
                                });
                            }
                            reader.onerror = function (evt) {
                                reject("error reading file");
                            }
                        }else {
                            resolve({
                                'email': email,
                                'header': header,
                                'content': content,
                                'selectedfile': null
                            });
                        }
                    });

With an Axios post i send the Data:

selectedfile = [0] filecontent, [1] filecontent from Filereader

to my PHP-Backend.

This is my PHP:

 $content = json_decode($request->getContent());

 $selectedfile = $content->selectedfile;

 file_put_contents("../storage/" . $selectedfile[1],$selectedfile[0]);
Elias
  • 125
  • 2
  • 22
  • Why send this as JSON in the first place? I’d perform a normal file upload, then you don’t need to determine any of the info you are looking for on the client side to begin with, you can do all that on the server side then. – CBroe Aug 04 '21 at 10:04
  • @Cbroe so you mean, to send the object of the to backend? – Elias Aug 04 '21 at 10:07
  • hi, can you show the code of your axios code please ? – erwan Aug 04 '21 at 10:10
  • 1
    I mean using FormData, and letting that do most of the work - https://stackoverflow.com/questions/5587973/javascript-upload-file – CBroe Aug 04 '21 at 10:14
  • @Cbrow ah okay, problem is that my html code is in a sweetalert popup, which means i have to go with a axios/ajax call cant use a form action – Elias Aug 04 '21 at 10:16
  • 1
    … and that is exactly what the question I linked to is about. The top answer shows how to use FormData with either fetch or a “classic” XHR. – CBroe Aug 04 '21 at 10:57
  • @CBroe so does fetch('/upload/image', {method: "POST", body: formData}); this line save the photo at "/upload/image" or what`? – Elias Aug 04 '21 at 11:46
  • No, that just performs the HTTP request to send the file to the server. The necessary next processing steps, still need to be handled by you. https://www.php.net/manual/en/features.file-upload.php – CBroe Aug 04 '21 at 11:48

0 Answers0