How to download a tar file received in HTTP Response using javascript? - javascript

I am getting a tar file in HTTP response body. I need to download the content as a tar file for the user. How can it be done in javascript?

There's nothing special about the scenario. Simply navigate to the URL.
window.location = 'http://example.com/your-tar-file.tar';

create a html link with the download attribute ,trigger the click attribute for that link
<a class="tar-generate" href="path/to/generated/tar" download=download>Download tar</a>
js for triggering :
$('.tar-generate').click();//trigger('click');
if this doesn't work you need to save that generated file to your server using file_put_content or any other file saving function, and then generate a download link

Make http request by passing the url, and give a callback function. In the Callback function write your logic
Example code:
function httpGetAsync(theUrl, callback)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
callback(xmlHttp.responseText);
}
xmlHttp.open("GET", theUrl, true); // true for asynchronous
xmlHttp.send(null);
}
//callback Function
httpGetAsync('https://api.github.com/users/abc', function (response) {
window.location = response.<tarLink> // here give the tar link
})

Related

How to download file into memory without writing the file to the system or without creating the file in a directory using javascript

I am looking for something like this, but using javascript. I want to download the file into javascript memory and then add the file content to a file input so that I can upload that again. I have written a function to access the file.
function fileDownload(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.send();
xhr.onload = function() {
if (xhr.status != 200) { // analyze HTTP status of the response
console.log(`Error ${xhr.status}: ${xhr.statusText}`); // e.g. 404: Not Found
} else { // show the result
callback(xhr.response)
}
};
}
So xhr supports the response type as text, arraybuffer, blob and document. So which type should I use? I want to support all types of files. Can I use blob?In that case how can I assign it to an input type of file. Please help.

chrome extension http request to website

I wanted to make a chrome extension that gets the html code from this website: https://free-proxy-list.net/
without actually going to that webpage.
I tried using the steps here:
https://developer.chrome.com/extensions/xhr
but the request kept showing as undefined when I tried to print it out.
The script I was using was:
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
alert(xhr.responseText);
}
}
xhr.open('GET', "url", true);
xhr.send(null);
document.write(xhr.send());
Note: not putting url in code since SO won't let me post a question with more than 2 links.
How would I get the code from this website in a string variable that I can parse?

JSON URL not working with Parameters

I have written the following code to read a JSON document from an external URL. This worked fine when the URL was the following:
http://localhost/EWSimpleWebAPI/odata/Users?
But NOT when I modified the URL as the following:
http://localhost/EWSimpleWebAPI/odata/Users?$filter=USER_NAME%20eq%20%27corpnet\anuwlk%27&$select=PROFILE
Javascript
var xmlhttp = new XMLHttpRequest();
var url = "http://localhost/EWSimpleWebAPI/odata/Users?$filter=USER_NAME%20eq%20%27corpnet\anuwlk%27&$select=PROFILE";
xmlhttp.open("GET", url, true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
myFunction(xmlhttp.responseText);
errorAlert("Status OKAY");
} else{
errorAlert("Status Not OKAY")
}
}
xmlhttp.send();
I'm retrieving the JSON Document thru a Web API using OData. OData accepts parameters in the URL and it worked fine in POSTMAN. I'm developing a Google Chrome extension and I'm not sure if it supports this URL with Parameters.
It would be best to use some function ( encodeURIComponent(str) and encodeURI(str) come to mind) to encode the parameters correctly.
As wOxxOm commented, your issue seems that one of the parameter has an unescaped character \.

Uploading a file meanwhile you keep interacting with the website

I got a web app working with a PHP MVC on the server side and javascript on the client side.
What I'm trying to do, is upload a file and meanwhile, do other tasks in the website until the response comes from the server.
Here bellow I post the code I'm using right now:
function xhr(url, data, callback) {
'use strict';
var request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (request.readyState === 4 && request.status === 200) {
callback('The content has been uploaded');
}
};
request.open('POST', url);
request.send(data);
}
The main problem here is when I request a new page, I don't know how to get the response.
Any help with this?.
If your site is not a single page application, once the user clicks on a link and gets redirected the upload progress might get canceled if it's not complete. If the upload to the server is complete at the time the user goes to another page, it will succeed but you will loose the reference to the object that has initialized the xhr object, thus, you won't be able to get the response. Now, What I would do, is take a look at the shared webworkers API.
You could delegate the file upload to the worker (which is a separate thread).
//define a worker
var worker = new Worker('worker.js');
worker.addEventListener('message', function (e) {
console.log('Worker said: ', e.data);
}, false);
worker.postMessage('Hello World'); // Send data to our worker.
And your worker.js file:
var request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (request.readyState === 4 && request.status === 200) {
self.postMessage(data);
}
};
request.open('POST', url);
request.send(data);

Simple code for request url and show response code | javascript | jquery

How can to request url or website address and show response code with javascript or jquery?
i.e
request www.google.com
if (response_code = 200) {
print "website alive"
} else if (response_code = 204) {
print "not found";
}
I'm assuming from the jquery tag that you mean to do this in a browser, not from a server running NodeJS or similar (although there is a NodeJS module for jQuery).
Although you can request URLs and see the response code using the XMLHttpRequest object, the Same Origin Policy will prevent your accessing virtually any sites other than the one the page itself was loaded from. But if you're pinging the server your page was loaded from to make sure it's still there, you can do that:
function ping(url, callback) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = handleStateChange;
xhr.open("get", url);
xhr.send();
function handleStateChange() {
if (xhr.readyState === 4) { // Request is complete
callback(xhr.status); // Tell the callback what the status code is
}
}
}

Categories