Downloads from Website to Mobile

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
JereMiami
Posts: 26
Joined: Mon Nov 03, 2014 12:17 am

Downloads from Website to Mobile

Post by JereMiami » Mon Jun 29, 2020 3:32 pm

I am wondering if there is a way to allow a mobile device to download a .txt file from a website like a desktop web browser does.

1) The website automatically transfers a .txt file to a desktop web browser upon executing javascript.
2) The mobile device, to my knowledge, does not download the .txt file when executing javascript using mobileControlDo "execute."
3) The mobile browser is a browser widget created by mobileControlCreate.

Is there any way for the mobile's web browser widget to download this .txt file?

Klaus
Posts: 12008
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Downloads from Website to Mobile

Post by Klaus » Tue Jun 30, 2020 2:23 pm

Hi JereMiami,

some questions:
Does the TXT file have a fixed URL?
If yes, you could simply download it "manually (with a script)" with "load url..." or "put url..."
We have write permission in the users Documents folder, so you should load it to
-> specialfolderpath("documents") & "/name_of_your_file.txt"
2) The mobile device, to my knowledge, does not download the .txt file when executing javascript using mobileControlDo "execute."
No, and I guess on the desktop there will be an alert before downloading the file, right?
Otherwise this would be a VERY HIGH security hole! :-)
3) The mobile browser is a browser widget created by mobileControlCreate.
There is a big difference between "the browser widget" and a native browser instance created with "mobilecreate...", like "mobilecontroldo..." does not work in a WIDGET!
So what are you actually using here?

Best

Klaus

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5603
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Downloads from Website to Mobile

Post by jacque » Tue Jun 30, 2020 5:30 pm

My mobile apps download text files without any problem. There is no warning and the download is very fast. I've never done it from within a browser though, it's done as Klaus suggests by scripting the download in LC, saving the file to the documents folder, and then setting the html of the native browser control to the content of the file.

Getting the file from the server is easy, just use the "put url" syntax:

Code: Select all

put url <url to server file> into url <path to documents folder>
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

JereMiami
Posts: 26
Joined: Mon Nov 03, 2014 12:17 am

Re: Downloads from Website to Mobile

Post by JereMiami » Sat Jul 25, 2020 9:18 pm

Thanks, I will give it a try and let you know!

JereMiami
Posts: 26
Joined: Mon Nov 03, 2014 12:17 am

Re: Downloads from Website to Mobile

Post by JereMiami » Sat Oct 17, 2020 3:07 pm

So I never was able to get this to work, unfortunately.

Here's what happens and what I am trying to do. No widgets involved. I use mobileControlCreate "browser" to open the native android browser.

First, I have the user navigate to a webpage. On that webpage is a form that is automatically completed and submitted using mobileControlDo "execute" (...). So far, so good.

On the desktop browser, a .txt file is automatically (without asking permission) downloaded. The .txt file gives a unique address specifying where the user will be redirected to next.

On android, however, it either: (1) does nothing after submitting the form (I have confirmed that the form is indeed submitted via mobileControlDo "execute"), or (2) it downloads the .txt file to somewhere that I am not familiar with. I simply cannot tell if it is downloading the .txt file and putting it somewhere that I have not been able to locate (it's not in the engine, resources, documents, cache etc.) or if it is simply not downloading the .txt file at all.

I would love, although not entirely necessary (but it would be super nice) to read that .txt file so I can automatically redirect the user to the new webpage. I am just unable to find it or download it through the native android browser (i.e., mobieControlCreate, not the widget). Any thoughts on how that can be done or where that pesky .txt file is?

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5603
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Downloads from Website to Mobile

Post by jacque » Sat Oct 17, 2020 5:58 pm

The file will be in the app's sandbox. File managers can't access the sandbox, but your app can. Where does your download handler put it? You can only write to specialfolderpath("documents") so that's where it needs to go.

To see the file, write a handler that retrieves it from the documents folder and displays it in an answer dialog or a special debugging field if it's very long.

Can you post the lines of script that do the download?
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

JereMiami
Posts: 26
Joined: Mon Nov 03, 2014 12:17 am

Re: Downloads from Website to Mobile

Post by JereMiami » Sat Oct 17, 2020 7:03 pm

This is Dropbox and I am revisiting this issue because of a recent change in their API choices (i.e., legacy vs. scoped). You can only use scoped now and by default the access token has a limited shelf life. So, I would really like to generate an access token for the user because the access token can be a bit challenging to achieve now.

Here's the portion of the script that has the .txt file issue:

Code: Select all

on browserFinishedLoading pUrl
   global gAccessToken
   put empty into gAccessToken
   if pUrl = "https://www.dropbox.com/developers/apps/create" then
      completeForm
   end if 
   end browserFinishedLoading
   
  on completeForm
   set the defaultFolder to specialFolderPath("documents")
   mobileControlDo sBrowserId, "execute", "document.getElementById('scoped').checked = true";
   mobileControlDo sBrowserId, "execute", "document.getElementById('scoped').click()";
   mobileControlDo sBrowserId, "execute", "document.getElementById('app_folder_permission').checked = true";
   mobileControlDo sBrowserId, "execute", "document.getElementById('app_folder_permission').click()"
   mobileControlDo sBrowserId, "execute", "document.forms['create-app-form']['name'].value += 'uniquenameforanapp'";
   wait 2 seconds
   mobileControlDo sBrowserId, "execute", "document.forms['create-app-form'].submit()";
   wait 2 seconds
   mobileControlDo sBrowserId, "execute", "document.forms['user-form'].submit()";
   end completeForm
Now, at this point, the desktop browser (Chrome) will automatically download the .txt file. The .txt file contains a portion of the url that it would redirect the user to for the user to generate the API access token (it would redirect the user if they clicked the button, but since we are submitting the form through javascript, we stay on the same page.) The .txt file comes from a different download link "https://www.dropbox.com/developers/apps/create/submit" (which is neither the same url as above nor the url the user would be redirected to). The site never directs you there, but rather just downloads the .txt file from that url (according to the desktop browser).

You can be sure because the following code does not work, which is attempting to "catch" the .txt file after submitting the form:

Code: Select all

 on browserFinishedLoading pUrl
if pUrl = "https://www.dropbox.com/developers/apps/create/submit" then
      put specialFolderPath("documents") & "/submit.txt" into tFileName
      put pUrl into url ("binfile:" & tFileName)
      if tFileName contains ".txt" then
         put it into URL ("file:"& tFileName)
      else
         put it into URL ("binfile:"& tFileName)
      end if
      put the result into tResult
      if tResult is empty then
         answer "Done"&cr&"File copied into Documents"
      else
         answer error tResult   
      end if
   end if
end browserFinishedLoading


So, I guess, what I am really asking is: "How do I catch that .txt file like it does on a desktop browser so I can access it?"

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5603
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Downloads from Website to Mobile

Post by jacque » Sat Oct 17, 2020 8:57 pm

I'm a little confused. This line:

Code: Select all

put pUrl into url ("binfile:" & tFileName)
puts a url string into a file in the documents folder. You don't really need to use binfile for that, but since you are, I wonder if you're trying to get the file content of that URL? If so, you'd want

Code: Select all

put url pUrl into url ("binfile:" & tFileName)
but your explanation indicates you do want the URL in the file.

What's in "it" when this line executes:

Code: Select all

put it into URL ("file:"& tFileName)
Remote debugging would be handy here, but you can also just put some temporary answer dialogs in the script if you want to see the values of the variables.

Code: Select all

 put specialFolderPath("documents") & "/submit.txt" into tFileName
      put pUrl into url ("binfile:" & tFileName)
      if tFileName contains ".txt" then
         put it into URL ("file:"& tFileName)
      else
         put it into URL ("binfile:"& tFileName)
      end if
TFileName will always contain ".txt" in this case, so no need for an "if" clause.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

JereMiami
Posts: 26
Joined: Mon Nov 03, 2014 12:17 am

Re: Downloads from Website to Mobile

Post by JereMiami » Sat Oct 17, 2020 11:55 pm

Thank you! I will have a go at this tomorrow morning and let you know how it goes.

JereMiami
Posts: 26
Joined: Mon Nov 03, 2014 12:17 am

Re: Downloads from Website to Mobile

Post by JereMiami » Sun Oct 18, 2020 1:28 pm

I'm a little confused. This line:
CODE: SELECT ALL

put pUrl into url ("binfile:" & tFileName)
puts a url string into a file in the documents folder. You don't really need to use binfile for that, but since you are, I wonder if you're trying to get the file content of that URL? If so, you'd want
CODE: SELECT ALL

put url pUrl into url ("binfile:" & tFileName)
but your explanation indicates you do want the URL in the file.
You are correct. I just want the .txt file in the "documents" folder to read from. But unfortunately we don't get to that point. Here's where I believe it ends.

Code: Select all

   mobileControlDo sBrowserId, "execute", "document.forms['create-app-form'].submit()";
   answer the result
Putting in "answer the result" after submitting the form gives us the javascript return "undefined," which means the form was submitted successfully. I can also can confirm that the form was submitted successfully from android because the project exits after we submit the form from android. So that is good.

Now, had we submitted the form on the desktop, we would have received what we see in the image attached to this post (a .txt file called "submit").

Image

When you look at the properties of this file, it tells you the download link is "https://www.dropbox.com/developers/apps/create/submit." So I thought to plug in the following code in the browserFinishedLoading command:

Code: Select all

on browserFinishedLoading pUrl
   global gAccessToken
   put empty into gAccessToken
   if pUrl = "https://www.dropbox.com/developers/apps/create/submit" then
      answer "File on its way!"
      put specialFolderPath("Documents") & "/submit.txt" into tFileName 
      ...
end browserFinishedLoading
I do not get a "File on its way!" dialog box after the answer command. It appears that the browser never loads the url of the download link. Is there anyway to get, or "catch," that .txt file? I've checked and its not in the sandbox (documents, engine, resources etc.). I have to assume its a browser permission or a line of code I am not thinking of the receive the .txt file. If not, no worries. We tried!
Attachments
lcforum.png

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5603
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Downloads from Website to Mobile

Post by jacque » Sun Oct 18, 2020 8:49 pm

I do not get a "File on its way!" dialog box after the answer command.
It sounds like pURL doesn't contain what you'd expect, so I'd add an answer dialog at the beginning of the handler to be sure it's returning the right URL:

Code: Select all

on browserFinishedLoading pUrl
   global gAccessToken
   ANSWER PURL -- does this match what you expect?
   put empty into gAccessToken
   if pUrl = "https://www.dropbox.com/developers/apps/create/submit" then
      answer "File on its way!"
      put specialFolderPath("Documents") & "/submit.txt" into tFileName 
      ...
end browserFinishedLoading
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”