0

Here is my problem, I would like to upload a file from a local path but i can't change input file value because it is impossible

The classical code of simple file upload :

HTML-code:

<input id="file" type="file" />
<progress id="progress"></progress>

JavaScript-code:

var fileInput = document.querySelector('#file'),
    progress = document.querySelector('#progress');

fileInput.addEventListener('change', function() {

    var xhr = new XMLHttpRequest();

    xhr.open('POST', 'upload.html');

    xhr.upload.addEventListener('progress', function(e) {
        progress.value = e.loaded;
        progress.max = e.total;
    }, false);

    xhr.addEventListener('load', function() {
        alert('Upload terminé !');
    }, false);

    var form = new FormData();
    form.append('file',         fileInput.files[0]);

    xhr.send(form);

  }, false);

How can we change it?

9
  • 1
    I'm confused, do you mean you want to know how to handle the upload request via AJAX? Commented Dec 28, 2014 at 23:58
  • I mean, select a file via JavaScript with a path that I know and upload the file with xhr request but i don't know how tout select a file with a path. Thanks un advance Commented Dec 29, 2014 at 0:03
  • Are you asking how to access the value of your <input>? Commented Dec 29, 2014 at 0:09
  • On @PM77-1 question: It is forbidden to retrieve the full path to a file due to security policies by modern browsers. A malicious coder could use this to retrieve information about your machine. Commented Dec 29, 2014 at 0:13
  • It is my problème! :) Commented Dec 29, 2014 at 0:23

1 Answer 1

2

You can't do it with pure JS. Modern browsers prevent you from direct work with the user's filesystem due to obvious insecurity. However, you can try to do it using Flash, ActiveX, Silverlight and so on.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.