I'm having issues with a small script I'm creating. It downloads large(ish) files from one domain (around 15Mb each) using AJAX, and I want to then upload / post them to another domain also using AJAX. Downloading isn't a problem. I've added a nice download status bar, and have checked that the responseText is stoed in a variable, which works fine.
The script then attempts to use AJAX POST to upload this data to another domain. I've set the 'Access-Control-Allow-Origin:' header on my domain, and the domains can both 'talk' to each other fine. Is it the AJAX that's not letting me upload such a large chunk of data? It actually crashes the browser.
Any ideas on how I should handle large files downloading then uploading straight away, would be greatly appreciated.
EDIT:
I'm trying to download a file from domain A to the users computer (but not save the file, just store it in a variable), then upload it straight to domain B.
Thanks! - Dan.
I would skip the downloading of the data through AJAX.
If i'm understanding what you're trying to do. You could download the data in your PHP, showing the progress using AJAX. Then within the same PHP code on the backend you could send the data to the next location. You could even send an signal through AJAX to tell your client side code that the download had finished and the sending had begun and have a progress bar for that.
Related
Currently, I have a PHP app running on Heroku using a Postgresql database. I want my users to be able to upload an image to a folder on my dropbox, and store other information (in this case, product information such as price, title, weight, location of the image on dropbox) on my database.
Right now, I'm using a file input inside an HTML form to submit the image by posting the whole form to my server (including the image), and then I use cURL to send the image to dropbox and wait for the response to succeed. On success, I create my database record that has the other information I mentioned earlier.
This works well for small files, but Heroku has a 30 second timeout that I can't change. For large files, the whole file uploads to the server, and then it uploads to dropbox. These two upload operations are time-intensive and takes more time than the timeout allows.
I had the idea of sending the file to dropbox using javascript (jQuery ajax commands specifically) so that it's handled by the client, and then POSTing to my server on success, but I'm worried about how secure that is since I would need to have my own authorization tokens in the source code that the client can view.
Is there any way for PHP to send a file from the client to an external URL without it touching the server? How do I do this securely?
This sounds like a good fit for the Dropbox API /2/files/get_temporary_upload_link endpoint.
You can call that on your server to retrieve the temporary upload link, and then pass that link down to the browser. You can then have some JavaScript code perform the upload directly from the browser using that link.
Since only the /2/files/get_temporary_upload_link endpoint call requires your Dropbox access token (whereas the temporary upload link itself doesn't), you can keep your access token secret on the server only, without exposing it to the client. And since the upload happens directly from the browser to the Dropbox servers, you don't have to pass the file data through your own server, avoiding the timeout issue.
I am making an android app and a website that downloads info from a php script. The php script compresses the data using gzcompress($dat, 9);.
I know how to uncompressed it in android, but how do you do it in javascript?
In this post:
Compress data in php and uncompress in javascript
sstringer mentions you can put a special header when using php's gzencode so jquery can automatically uncompress it. Is there a way for the way I am doing it?
Thanks
I looked into this myself once. I decided to go to the ajax route and then redirect.
If you know where the file is being stored on the server you could post that to a php script which decompresses it and saves the file somewhere. Next, return this in the ajax response and then just redirect the user to the saved, decompressed file.
There is a project on GitHub: https://github.com/gcanu/gzip.js
That might be what you are looking for.
No matter how much I look this up all I get is the w3C File API which focuses on local files for the client.
What I'm trying to do is I have a server. I'm trying to use client-side javascript to grab the server hosted text file, a.txt, and display it to the innerDOM of an html page. My server directory look like this:
index.html
read.js
text files
a.txt
All I want to have happen is for, on the client side, the javascript read.js running in the index.html on onload to display the contents of a.txt. I figure that since a.txt will never be large, leaving it to the client is fine.
But I can't figure out how to do this and the W3C File API isn't offering me answers.
If I had to guess, somehow making sure index.html loads a.txt and then grabbing that via the file API might be the way to go but I'm not sure how to do that.
And I'll admit it, I'm a bit of a noob. If I'm invalidating browser sandbox or doing something impossible, please tell me. I just thought this would be so simple.
Also, I'd appreciate that if you were going to suggest AJAX, either don't, or explain it like I'm a baby because I really don't know.
Thank you all so much for your help.
Why file API is irrelevant:
Web applications should have the ability to manipulate as wide as possible a range of user input, including files that a user may wish to upload to a remote server or manipulate inside a rich web application.
From W3C File API.
So, File API is intended to be used to allow users to upload files from their clients into the server. On the other hand, AJAX is used to allow users to download files and other data from the server into their clients. And this is exactly what you need.
Refer to jQuery's ajax documentation.
I believe this page should help you out with your problem.
http://www.htmlgoodies.com/beyond/javascript/read-text-files-using-the-javascript-filereader.html#fbid=YhNukIHynD3
I would suggest using an Ajax call to the file on the server, since the response of the call will typically be the contents of that file.
Using Jquery this can be done by a simple
$.ajax({ 'url':'a.txt',
'success': function(r){
//display to innerDOM here, using r as the file
});
});
You simply want to display a txt file on the web page?
Do you know about server side includes?
That would be one possibility if you control the server.
If you really want to do it in javascript, then AJAX would be the way to go.
If it were me at that point I would figure out how to include and use jQuery to help with the ajax bits.
You will simply request the text file via its URL (you can get it to load in the browser right?), and then use jQuery to put that text into some DOM element.
Is it possible, on any browser, to indicate the percent complete of a file upload without requesting that information from the server?
In other words, is there any browser which knows (in a javascript-accessible way) how much of a file it has uploaded?
Browser file uploads don't provide a scriptable handle for JavaScript.
Typically how this is done is via a an AJAX call to the server, which then passes back the data based on the server's file upload status (which can be scripted). I don't think this can be done on the client side alone.
We're using GWT and want to download an image to the client, then send this image to another server using a POST request...
A JavaScript/AJAX solution would be helpful too.
Is this plausible? Can a web app access cached images from the client's browser?
We're rookies, if this is futile please let us know. Thanks!
UPDATE: We abandoned this approach. After doing some research it seems that the only way one can set a multipart body request is through an HTML form, even then the client has to specify what file she wants uploaded. It makes perfect sense from a security standpoint. What we were trying to do was basically route an image image from server to client to server... thanks for the help.
You could use a privileged Applet or Flash, but basic JavaScript won't do it.
Or maybe just send the image URL to the foreign server with JSONP and let that server download the image itself?
Ajax, or asynchronous get/post cannot cross domains. You can only achieve this by having a hidden form, modifying the action parameter, and then using javascript to execute the submit on that form.
I'm sorry to say, I don't know how to go about that in GWT (been a long time since i used it), but that's the algorithm.