XMLHttpRequest to read an external file

后端 未结 2 1917
心在旅途
心在旅途 2021-01-20 15:48

I want to retrieve the data contained in a text file (from a given URL) through JavaScript (running on the client\'s browser).

So far, I\'ve tried the following appr

2条回答
  •  醉酒成梦
    2021-01-20 16:14

    var xhr = new XMLHttpRequest();
    xhr.open('POST', '/uploadFile'); 
    var form = new FormData();
    form.append('file', fileInput.files[0]);
    xhr.send(form);
    

    It was previously impossible to upload binary data with XMLHttpRequest object, because it could not stand the use of FormData (which, anyway, did not exist at that time) object. However, since the arrival of the new object and the second version of XMLHttpRequest, this "feat" is now easily achievable

    It's very simple, we just spent our File object to a FormData object and upload it

提交回复
热议问题