Javascript - how to identify a file after an upload to the server

Asked

Viewed 50 times

0

Hello, I have a script where the user can upload a .pdf. Each time it is done, the file is saved in the /tmp folder with a sequential Generic name , different from the original name. I need a way to identify the file. I tried to change the name of the file saved in fd.append , but only changes the extension. Or better still create a text file with some hints about the upload and save next to the file, to be able to read later.

<script type="text/javascript">
                function fileSelected() {
                    var file = document.getElementById('fileToUpload').files[0];
                    if (file) {
                        document.getElementById('fileName').innerHTML = 'Name: ' + file.name;
                        document.getElementById('fileType').innerHTML = 'Type: ' + file.type;
                        document.getElementById('upload').style.display= 'block';
                    }
                }

                function uploadFile() {
                    var fd = new FormData();
                    fd.append("fileToUpload", document.getElementById('fileToUpload').files[0]);
                    var xhr = new XMLHttpRequest();
                    xhr.addEventListener("load", uploadComplete, false);
                    xhr.addEventListener("error", uploadFailed, false);
                    xhr.open("POST", "", true);
                    xhr.send(fd);
                    document.getElementById('upload').style.display= 'none';
                }


                function uploadComplete(evt) {
                    document.getElementById('progresso').innerHTML = 'Upload completo !';
                    document.getElementById('upload').style.display= 'block';
                }

                function uploadFailed(evt) {
                    document.getElementById('upload').style.display= 'block';
                    alert("Houve um erro ao tentar subir o arquivo");
                }           
                </script>

1 answer

0

Hello,

You cannot upload with pure Javascript, without the server receiving the files and processing them! You need to handle in the BACK-END!

No defined URL

xhr.open("POST", "", true);

You need a page that captures and treats information!

See an example of the PHP page: Upload File using PHP

  • Hi Obg for the answer . Unfortunately it is an old system , without the proper support, in a closed server , with own web server. I cannot activate PHP in this environment. Otherwise there would be the problem , my problem is trying to identify these files , within the javascript that it allows. As I said in the post , I can change the file extension saved , so it is not inviolable,

  • I’m sorry! Without the back-end I can’t imagine a solution! It makes uploading treatment...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.