XHR request changes response based on file extension - javascript

I have a binary file that contains an Uint8 array [0,1,2,3,4,5].
If I name it test.txt and load it everything is fine. However if I change its extension name to test.bpg the loaded data is [1,4].
What could make this happen? It doesn't happen when I run the file on local host (note only firefox will let me do this because of cross origin security).
However when I upload it to the server the strange responses start happening. I never configured the server to do anything special with either .txt files or .bpg files. It's just a standard nginx install.
In case you want to see some code I am retrieving that file like this:
var request = new XMLHttpRequest;
request.open("GET", 'test.txt', true);
request.responseType = "arraybuffer";
request.onload = (function(event) {
var data = request.response;
var array = new Uint8Array(data);
console.log(array);
});
request.send(null)
Any help would be much appreciated!

Related

Rss will not parse

I've been experimenting with xml data but I found an xml file with a structure that I've never seen before. I tried to call it using php and log it in the console but no luck any idea as to why? When I try this method with other files there seems to be no issue, for example, if you replace the url with this one "http://news.google.com/news?ned=us&topic=h&output=rss" it works fine. Code is below
PHP
$xml = "https://w1.weather.gov/xml/current_obs/display.php?stid=KATL";
echo file_get_contents($xml);
JS
var xhr = new XMLHttpRequest();
xhr.open("GET", "metar.php");
xhr.onload = function (response) {
var res = this.response;
console.log(res);
}
xhr.send();
This is not a problem with your script, but with the resource you are requesting. The web server is returning the "forbidden" status code.
You should probably check the https and the url.

Firestorage downloading from download url doesn't download my file

I am fetching a file via the download URL as described here
https://firebase.google.com/docs/storage/web/download-files
This is the code I used
var xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.onload = function(event) {
var blob = xhr.response;
};
xhr.open('GET', this.$data._downloadURL);
xhr.send();
And I get a response with status 200 and in the preview I can see the image. But it doesn't seem to be downloading in my browser. How can I do that?
I tried putting a cors json file in the storage as well but it doesn't seem to have any imact.
You have not told the script to do anything with the data. The part of your code that reads
xhr.onload = function(event) {
var blob = xhr.response;
};
runs when the download is complete. Your data is in the blob variable. If you want to download it, you can try for instance the techniques described here inside the xhr.onload function.

How do I upload the file at a URL using XHRWebRequest?

In a chrome extension I have a url stored as ctx.srcUrl. I am trying to upload the file that the URL points to onto a server. I can upload a file object using XHR with:
function uploadFile(file) {
var xhr = new XMLHttpRequest()
var formData = new FormData()
formData.append('upload', file)
xhr.open('POST', 'https://endpoint.com/upload')
xhr.withCredentials = true
xhr.send(formData)
}
Where file is a Javascript File object (https://developer.mozilla.org/en-US/docs/Web/API/File). I can also download an object using:
function downloadFile() {
const request = new XMLHttpRequest()
request.open('GET', ctx.srcUrl, true)
request.responseType = 'blob'
request.onload = function() {
// response is stored in `request.response`
}
request.send()
}
I'm struggling to work out how to link them up in an efficient manner however, preferably without keeping all of the response in memory at once (using streaming)? The best way I can find of doing it is using FileReader.readAsDataURL() but that has a file limit of ~256MB and some files might be larger than that.
There must be a better way of doing this, can anyone point me in the right direction?

Download blob sometimes fails

I have a chrome app that blocks user's downloads and my code will instead download it in safe way. I want to download blob files with js; my code has worked fine until now, but I have found a blob link that my code fails on and I can't find why. Here is my code:
var request = new XMLHttpRequest();
request.open('GET', uri, true);
request.responseType = 'blob';
request.onload = function (evt) {
var reader = new FileReader();
reader.readAsDataURL(request.response);
reader.onload = function (e) {
var b64 = e.target.result.split("base64,")[1];
var formData = new FormData();
};
};
request.send();
My code based on this answer.
The file that I'm trying to download via js:
This is the website that contains the link
And here is the url link to the blob:
blob:http://worldpopulationreview.com/b18cab08-e62e-47e5-8e31-413f2e73f72d
The error:
GET blob:http://worldpopulationreview.com/b18cab08-e62e-47e5-8e31-413f2e73f72d net::ERR_FILE_NOT_FOUND
Any ideas?
There error means that the file can not be found. This means that the address you are trying to download from doesn't exist on the website.
Try accessing the page via your browser.
Link to the page
You'll see that it returns a 404 error. This means that the URL does not exist.
More info about error 404 here.
This error has nothing to do with your javascript code.

Can't send file using XMLHttpRequest

The code is simple and it works when I append a string to the FormData:
var data = new FormData();
data.append('String', stringVar);
var request = new XMLHttpRequest();
request.open('POST', 'upload.php');
request.send(data);
But if I append a file to the FormData (data.append('File', fileVar);) I get a 503 response.
The problem doesn't happen when I'm running the code on localhost, so I know there's no problem with the code and it's from the server. Maybe I should change some PHP or Apache settings on the server, I don't know.
Any help or comments would be appreciated.

Categories

Resources