1

I want to let user select a file. The path location should be stored in a JavaScript string. later after validating i want to make a ajax call to server to upload the file on to the server using PHP.

I don't want to submit form directly as page would reload and there are many sql queries executed and would be unnecessary load on server.

    <input type="file" id="files" name="files[]" multiple />
    <output id="list"></output>

    <script>
      function handleFileSelect(evt) {
        var files = evt.target.files; // FileList object

        // files is a FileList of File objects. List some properties.
        var output = [];
        for (var i = 0, f; f = files[i]; i++) {
          alert(JSON.stringify(f));
          output.push('<li><strong>', escape(f.name), '</strong> (', f.type || 'n/a', ') - ',
                      f.size, ' bytes, last modified: ',
                      f.lastModifiedDate ? f.lastModifiedDate.toLocaleDateString() : 'n/a',
                      '</li>');
        }
        document.getElementById('list').innerHTML = '<ul>' + output.join('') + '</ul>';
      }

      document.getElementById('files').addEventListener('change', handleFileSelect, false);
    </script>

This alert gives me details but not the path that i want.

Any help on getting the path so i can just pass the string via ajax using jquery and upload file with php using move_uploaded_file.

Thanks in advance.

user2626445
  • 1,021
  • 12
  • 26
  • What are you trying to achieve? Uploading a file via AJAX? You can not just pass a string via AJAX, you need to actually upload the file. – putvande Jul 27 '13 at 22:48
  • what you're trying to achieve is not possible for security reason the browser never returns this. You can find the answer here : http://stackoverflow.com/questions/15201071/how-to-get-full-path-of-selected-file-on-change-of-input-type-file-using-jav – scraaappy Jul 27 '13 at 22:49
  • 1
    @putvande Hi sir i want the file path as a string and make a ajax request to upload the file.I have no idea if this can be done – user2626445 Jul 27 '13 at 22:57
  • @scraaappy Is there any way to submit form without page reload? – user2626445 Jul 27 '13 at 22:58
  • Have a look at this post http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery/8758614#8758614. Maybe you can find your solution there. – putvande Jul 27 '13 at 22:59

1 Answers1

0

By the time, the user has made a selection, you ought to have a value. Just find somewhere to store it and then pull it back in where you need it. If you don't want to submit a form just to get the value, you could include a select/option combo from where to fetch the value before continuing to the actual form. No selection = no form. Selection = form, but not yet submitted.