Threaded View
-
25 Feb 2013 2:20 PM #1
Answered: Question - download file to predefined location
Answered: Question - download file to predefined location
Hi,
I have a question about Sencha Desktop Packager regarding downloading of files.
I want to build an application where the user can select an online file, edit it on the local computer, and submit/upload it when the file is changed. In the support pages I can find how to open a file, and monitor the file/folder when it changes. Uploading can be done using regular HTML.
But I can't find the option to download the selected file to a (temporary) location. When I just link to the file, I don't know what the location will be, so that's not an option.
Is it possible to let my app download a file to a location on the user's computer without the user having to choose the location?
Thanks in advance.
Greetings, vosManz
-
Best Answer Posted by jarrednicholls
Great question. Currently the way to save a file to disk from a remote URL would be to use XHR, download the binary data, and then save that binary data by passing it in a format compatible with Ion.io.writeFile, like so:
Note that this requires the "allowCrossSite" security manifest setting to be enabled to bypass x-domain security policies. Also, if you are dealing with text files, you can instead just save the xhr.responseText and pass that directly to Ion.io.writeFile as a string, instead of saving it as binary data:Code:function saveFileToDisk(url, dest, callback) { var xhr = new XMLHttpRequest(); xhr.onload = function() { var typedData = new Uint8Array(xhr.response), ln = typedData.length, data = new Array(ln), result; for (var i = 0; i < ln; i++) { data[i] = typedData[i]; } result = Ion.io.writeFile(dest, data); callback(result.success); }; xhr.onerror = function() { callback(false); }; xhr.open('GET', url); xhr.responseType = 'arraybuffer'; xhr.send(null); }
This generally is a pain point right now with very large binary files. For files that are < 15-20mb it is generally acceptable. We are working to enhance our file I/O APIs to add in asynchronous streaming and compatibility with the Typed Arrays specification.Code:result = Ion.io.writeFile(dest, xhr.responseText);
You can listen to click events on those particular file hyperlinks, and handle that click event by saving the file to disk. You could attach a click handler directly on the hyperlinks...
...or use the cleaner way and catch all clicks on hyperlinks...Code:<a href="http://some/url/to/my/file.txt" onclick="saveFileToDisk(this.href, Ion.io.tempPath + 'file.txt', function(){}); return false;">Click Here!</a>
Hope that helps.Code:document.body.addEventListener('click', function(e) { var t = e.target; if (t.tagName == 'A' && t.dataset.fileName) { e.preventDefault(); saveFileToDisk(t.href, Ion.io.tempPath + t.dataset.fileName, function(success) { if (success) // do something else // do something }); } }, true); <a href="http://some/url/to/my/file.txt" data-fileName="file.txt">Click Here!</a> <a href="http://some/url/to/my/file2.txt" data-fileName="file2.txt">Click Here!</a> etc.
Thank you for reporting this bug. We will make it our priority to review this report.


Reply With Quote