10 examples of 'javascript read file from url' in JavaScript

Every line of 'javascript read file from url' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
5function readFile(url, callback) {
6 var request = new XMLHttpRequest();
7 request.onreadystatechange = function() {
8 if (request.readyState === 4) {
9 return callback(request.responseText);
10 }
11 };
12
13 request.open('GET', url, false);
14 request.send(null);
15}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

116function readURL(input)
117{
118 if (input.files && input.files[0])
119 {
120 var reader = new FileReader();
121 reader.onload = function (e)
122 {
123 $('#photo_preview').attr('src', e.target.result);
124 }
125 reader.readAsDataURL(input.files[0]);
126 }
127}
74function readURL(input) {
75 if (input.files && input.files[0]) {
76 var reader = new FileReader();
77 reader.onload = function(e) {
78 var image = new Image();
79 image.src = e.target.result;
80 image.onload = function(ev) {
81 $('#avatar-update').attr('src', this.src);
82 if (!$('.avatar-update-container').find('.jcrop-holder').length) {
83 initJcrop(this.width, this.height);
84 } else {
85 jcrop_api.setImage(this.src, function() {
86 this.setSelect([
87 0,
88 0,
89 jcrop_api.getBounds()[0],
90 jcrop_api.getBounds()[1],
91 ]);
92 });
93 }
94 };
95 };
96 reader.readAsDataURL(input.files[0]);
97 }
98}
1function readURL(input) {
2 if (input.files && input.files[0]) {
3 var reader = new FileReader();
4
5 $('.upload').attr('disabled','enabled');
6 reader.readAsDataURL(input.files[0]);
7
8 reader.onload = function (e) {
9 $('#blah')
10 .attr('src', e.target.result)
11 .show();
12 };
13 }
14}
23function readFile (url) {
24 return new Promise(function (done, reject) {
25 fs.readFile(url, function (err, data) {
26 console.log('JODER"', data.toString().slice(0, 10))
27 err ? reject(err) : done(data)
28 })
29 })
30}
16function readFileArrayBuffer(url, cb){
17 var request = new window.XMLHttpRequest();
18 request.open('GET', url, true);
19 request.responseType = 'arraybuffer';
20
21 // Decode asynchronously
22 request.onload = function() {
23 cb(null, request.response)
24 }
25 request.onerror = cb
26 request.send();
27}
19function readURL2(input) {
20
21 if (input.files && input.files[0]) {
22 var reader = new FileReader();
23
24 reader.onload = function (e) {
25 $('#imageDisplay2').attr('src', e.target.result);
26 }
27
28 reader.readAsDataURL(input.files[0]);
29 }
30}
47function readFileSync (url: string): string {
48 const xhr = new XMLHttpRequest()
49 xhr.open('GET', url, false)
50 xhr.send()
51 if (xhr.status < 200 || xhr.status >= 300) {
52 throw new Error(xhr.statusText)
53 }
54 return xhr.responseText as string
55}
11function getContentsFromFile(url) {
12 return $.ajax({
13 type : "GET",
14 url : url,
15 dataType : "string",
16 async : false,
17 success : function(data) {
18 shader = data;
19 }
20 }).responseText;
21}
63readFromUrl(url) {
64 return new Promise((resolve, reject) => {
65 this.m_url = url;
66 this.m_request = null;
67 const METHOD = 'GET';
68 this.m_request = new XMLHttpRequest();
69 if ('withCredentials' in this.m_request) {
70 // this.m_request.withCredentials = true;
71 const NEED_ASYNC = true;
72 this.m_request.open(METHOD, this.m_url, NEED_ASYNC);
73 } else if (typeof XDomainRequest !== 'undefined') {
74 console.log('HttpRequest: XDomainRequest will be used');
75 this.m_request = new XDomainRequest();
76 this.m_request.open(METHOD, this.m_url);
77 } else {
78 this.m_request = null;
79 console.log('This browser cant support CORS requests');
80 return;
81 }
82 this.m_request.responseType = 'arraybuffer'; // "blob"
83 this.m_request.addEventListener('load', (event) => {
84 const arrBuf = event.target.response;
85 if (arrBuf === null) {
86 console.log('Bad response type. Expect object type in response.');
87 } else {
88 resolve(arrBuf);
89 }
90 }, false);
91
92 this.m_request.addEventListener('error', (event) => {
93 const strError = `Error event happend for XMLHttpRequest: loaded = ${event.loaded}, total = ${event.total}`;
94 console.log(strError);
95 reject(strError);
96 }, false);
97 this.m_request.send();
98 });
99}

Related snippets