Recent

Author Topic: How to download a file from web page displayed by jWebView  (Read 11461 times)

c4p

  • Full Member
  • ***
  • Posts: 166
Re: How to download a file from web page displayed by jWebView
« Reply #30 on: June 05, 2023, 08:52:01 pm »
It's definitely a permissions issue, if you go to your app in Settings on Android, go to Apps select your app and go to Permissions, then Files and Media, it's probably set to 'Allow access to media only' select 'Allow management of all files'.
You should now be able to access /Download folder and open xlsx files using StartDefaultActivityForFile etc.
I am using Android 12, so manually granting your app all files permissions may be slightly different on your device.

This works now on my device after I set the permissions manually on the device to Allow management of all files:

Code: Pascal  [Select][+][-]
  1. Self.StartDefaultActivityForFile(Self.GetEnvironmentDirectoryPath(dirSdCard)+'/Download/'+URLFilename,'application/vnd.ms-excel');

Of course, this is a proof of concept to show it's a permissions issue, but you now have to grant access to this permission via code.

I will look for the code to grant permission to all file via code, probably ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION via an intent.
« Last Edit: June 05, 2023, 08:53:57 pm by c4p »
Lazarus 2.0.12 r64642/FPC 3.2.0 LAMW v0.8.6.4 on Windows 10+Linux Mint 21.2, projects mainly built using AppCompat and Gradle.

maurobio

  • Hero Member
  • *****
  • Posts: 623
  • Ecology is everything.
    • GitHub
Re: How to download a file from web page displayed by jWebView
« Reply #31 on: June 05, 2023, 09:19:14 pm »
Hi, @c4p!

Thanks, but this gets more and more (there is always a "but", and several "mores") weirder...

First of all, I can't understand the permissions issue I find especially difficult to understand why this crappy, half-breed operating system which is called 'Android' allows to download (i.e., writing) files to the damned main storage device but does not allow to open (i.e., reading) the very same files! This defies all logic!

Anyway, isn't there one or more ways of granting permissions via code? Thousands of apps ask users for permissions before running the first time, why this very silly app of mine cannot do the same?

And more (!): in your code you are using the SDCard, bur the problem is not supposed to be with the main storage device? What if the poor user doesn't have a SDCard?

And yet more: if the problem is really one of the permissions, why not simply download the file to a 'public' folder or something where the downloaded files can be opened without hassle?

I am totally confused!

With best regards,
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 2.0.12 - FPC 3.2.0 on GNU/Linux Mint 19.1, Lubuntu 18.04, Windows XP SP3, Windows 7 Professional, Windows 10 Home

c4p

  • Full Member
  • ***
  • Posts: 166
Re: How to download a file from web page displayed by jWebView
« Reply #32 on: June 05, 2023, 09:38:20 pm »
If it was me, I would download the file to the project folder, unless you have a reason why you need to download to /Download, there may be a valid reason why you want to do this, easy access to files for the user etc. I personally find dealing with permissions a bind/messy and always keep within the confines of my project folder, less problems for you and the user, some people panic when they see a permissions box in an app.
Now, if you could change the JDownloadManager download path to your project path it would be problem solved, I honestly don't know if you can, but I will check, maybe jmpessoa knows.

dirSDCard is a legacy assignment and actually points to the root of the devices storage. I think something like dirextSDCard was an actual sdcard.

I agree with you about Android but it has gone this way because of exploits and people not knowing what's happening with their data, so it gives the user more control over their data, an absolute pain for anyone coding an android though!

Now to play with jDownloadManager!
Lazarus 2.0.12 r64642/FPC 3.2.0 LAMW v0.8.6.4 on Windows 10+Linux Mint 21.2, projects mainly built using AppCompat and Gradle.

maurobio

  • Hero Member
  • *****
  • Posts: 623
  • Ecology is everything.
    • GitHub
Re: How to download a file from web page displayed by jWebView
« Reply #33 on: June 05, 2023, 09:51:14 pm »
Hi, @c4p!

Thanks, as always, for your clear and comprehensive explanation!

From my part, I am also attempting to do something to help ourselves - I am developing a "demo"as jmpessoa likes, sort of a simplified version of my own app. I presume it will be much easier for us to work on the basis of it.

I will post it in an attachment in the next half-hour.

With warmest regards,
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 2.0.12 - FPC 3.2.0 on GNU/Linux Mint 19.1, Lubuntu 18.04, Windows XP SP3, Windows 7 Professional, Windows 10 Home

maurobio

  • Hero Member
  • *****
  • Posts: 623
  • Ecology is everything.
    • GitHub
Re: How to download a file from web page displayed by jWebView
« Reply #34 on: June 05, 2023, 10:12:54 pm »
Hi, @cp4!

As promised, here is my complete "demo" ( >:D) app, which is a striped-down version of my production app.

When executed, it simply displays a pre-defined webpage with three links to files in XLSX, XLS, and PDF formats (these are the only types my production app will have to deal with).

Clicking on a link causes the file to be download (it it does not exist) and then the file is (or should be!) opened in the associated app.

In principle, it does not matter in what folder the downloaded file will be stored, provided that the user can open it in the associated app.

Hope we can use this code as a testbed to finally come up with a complete working solution.

With warmest regards,

UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 2.0.12 - FPC 3.2.0 on GNU/Linux Mint 19.1, Lubuntu 18.04, Windows XP SP3, Windows 7 Professional, Windows 10 Home

jmpessoa

  • Hero Member
  • *****
  • Posts: 2317
Re: How to download a file from web page displayed by jWebView
« Reply #35 on: June 05, 2023, 10:25:01 pm »
Quote
Now to play with jDownloadManager!...

Yes! the demo " AppDownloadManagerDemo1"  has all we need....  (to  access the URI of the location where was saved your file, handle the event "OnReceiver"  as shown in the demo.....)

@maurobio
Please, check what you got on "URL" parameter  received from "jWebView1Status"  event...

if  the link  is all ok,  then send me the link to a very simple file.... (if private,  sent it by message or e-mail....)
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

maurobio

  • Hero Member
  • *****
  • Posts: 623
  • Ecology is everything.
    • GitHub
Re: How to download a file from web page displayed by jWebView
« Reply #36 on: June 05, 2023, 10:31:26 pm »
Hi, @jmpessoa!

Please see the demo app I attached to my previous post in reply to @cp4! It is a shortened version of my production app - if we get it working, so our problems are solved (until the next ones appear)!

With best regards,
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 2.0.12 - FPC 3.2.0 on GNU/Linux Mint 19.1, Lubuntu 18.04, Windows XP SP3, Windows 7 Professional, Windows 10 Home

c4p

  • Full Member
  • ***
  • Posts: 166
Re: How to download a file from web page displayed by jWebView
« Reply #37 on: June 05, 2023, 10:32:12 pm »
Actually, I think DownloadService is probably better (see AppDownloadServiceDemo1) as I could not get jDownloadManager outside the confines of the /Download directory (this may be by design though), DownloadService will download direct to your project folder, have a look at this code and adapt for your own:

Add jBroadcastReceiver
Add jDownloadService
Change YOURAPPNAME to your app name, whatever your project folder is called, probably.

Code: Pascal  [Select][+][-]
  1.  BroadcastReceiver1.RegisterIntentActionFilter('org.lamw.YOURAPPNAME.DOWNLOAD_RECEIVER'); //Register intent for Broadcast receiver
  2.  DownloadService1.SaveToFile(Self.GetEnvironmentDirectoryPath(dirDownloads), 'sample-1.xlsx'); //Save file to project Download directory
  3.  DownloadService1.Start('https://getsamplefiles.com/download/xlsx/sample-1.xlsx', 'org.lamw.YOURAPPNAME.DOWNLOAD_RECEIVER'); //Start the download


Put below into OnReceiver event of jBroadcastReceiver.
Code: Pascal  [Select][+][-]
  1.  Self.StartDefaultActivityForFile(Self.GetEnvironmentDirectoryPath(dirDownloads) + '/sample-1.xlsx','application/vnd.ms-excel'); //Start the downloaded file using correct mime type

@maurobio probably something like this for your app using excel mime type, obviously you would need to do a check to see if the downloaded file is excel, pdf, txt extension etc. and assign correct mime type for the downloaded file type.

Code: Pascal  [Select][+][-]
  1.  BroadcastReceiver1.RegisterIntentActionFilter('org.lamw.YOURAPPNAME.DOWNLOAD_RECEIVER'); //Register intent for Broadcast receiver
  2.  DownloadService1.SaveToFile(Self.GetEnvironmentDirectoryPath(dirDownloads), URLFilename); //Save file to project Download directory
  3.  DownloadService1.Start(URL, 'org.lamw.YOURAPPNAME.DOWNLOAD_RECEIVER'); //Start the download


Put below into OnReceiver event of jBroadcastReceiver.
Code: Pascal  [Select][+][-]
  1.  Self.StartDefaultActivityForFile(Self.GetEnvironmentDirectoryPath(dirDownloads) + '/'+URLFilename,'application/vnd.ms-excel'); //Open the downloaded file using correct mime type/app

You shouldn't need permissions, although you can add permission checks if you wish, but not needed really as you will have full access to the Download directory in your own project area.
« Last Edit: June 05, 2023, 11:02:59 pm by c4p »
Lazarus 2.0.12 r64642/FPC 3.2.0 LAMW v0.8.6.4 on Windows 10+Linux Mint 21.2, projects mainly built using AppCompat and Gradle.

maurobio

  • Hero Member
  • *****
  • Posts: 623
  • Ecology is everything.
    • GitHub
Re: How to download a file from web page displayed by jWebView
« Reply #38 on: June 05, 2023, 10:42:17 pm »
Hi, @cp4!

I hope we are getting there...

Quote
@maurobio probably something like this for your app using excel mime type, obviously you would need to do a check to see if the downloaded file is excel, pdf, txt extension etc. and assign correct mime type for the downloaded file type.

I added these checks to the demo app I sent you in a previous reply. It looks like with a few minor modifications we can cat this thing done - but the demo ( >:D) is always in the details...

Quote
Change YOURAPPNAME to your app name, whatever your project folder is called, probably.

An now, for a very funny question! What is exactly the name of my app?  :D In the case of the demo I sent you, it is called ShowDownApp, but in the device it is titled AppLAMWProject1. Is it suffering from an identity crisis?

With warmest regards,
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 2.0.12 - FPC 3.2.0 on GNU/Linux Mint 19.1, Lubuntu 18.04, Windows XP SP3, Windows 7 Professional, Windows 10 Home

jmpessoa

  • Hero Member
  • *****
  • Posts: 2317
Re: How to download a file from web page displayed by jWebView
« Reply #39 on: June 05, 2023, 10:49:34 pm »
Quote
Code: Pascal  [Select][+][-]
  1.  DownloadService1.Start('https://getsamplefiles.com/download/xlsx/sample-1.xlsx', 'org.lamw.YOURAPPNAME.DOWNLOAD_RECEIVER');
  2.  
  3.  Self.StartDefaultActivityForFile(Self.GetEnvironmentDirectoryPath(dirDownloads) + '/sample-1.xlsx','application/vnd.ms-excel');
  4.  

No!   

Put  this code in "jBroadcastReceiver1Receiver"  (as shown in "demo"...):
Code: Pascal  [Select][+][-]
  1.  Self.StartDefaultActivityForFile(Self.GetEnvironmentDirectoryPath(dirDownloads) + '/sample-1.xlsx','application/vnd.ms-excel');
  2.  


That is: the result from "DownloadService"  or "DownloadManager"   need be handled by events.....
« Last Edit: June 05, 2023, 10:52:10 pm by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

c4p

  • Full Member
  • ***
  • Posts: 166
Re: How to download a file from web page displayed by jWebView
« Reply #40 on: June 05, 2023, 10:59:22 pm »
Yes, of course, that would be better, I have amended the post.
Too much speed  :)

Put below into OnReceiver event of BroadcastReceiver.
Code: Pascal  [Select][+][-]
  1.  Self.StartDefaultActivityForFile(Self.GetEnvironmentDirectoryPath(dirDownloads) + '/sample-1.xlsx','application/vnd.ms-excel');
« Last Edit: June 05, 2023, 11:01:29 pm by c4p »
Lazarus 2.0.12 r64642/FPC 3.2.0 LAMW v0.8.6.4 on Windows 10+Linux Mint 21.2, projects mainly built using AppCompat and Gradle.

maurobio

  • Hero Member
  • *****
  • Posts: 623
  • Ecology is everything.
    • GitHub
Re: How to download a file from web page displayed by jWebView
« Reply #41 on: June 05, 2023, 11:13:52 pm »
Hi, @c4p, @jmpessoa!

Can all these modifications be implemented in the "demo" app I sent in a previous reply? This will make things clear (to me, at least).

Thanks in advance!

With warmest regards,

UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 2.0.12 - FPC 3.2.0 on GNU/Linux Mint 19.1, Lubuntu 18.04, Windows XP SP3, Windows 7 Professional, Windows 10 Home

maurobio

  • Hero Member
  • *****
  • Posts: 623
  • Ecology is everything.
    • GitHub
Re: How to download a file from web page displayed by jWebView
« Reply #42 on: June 06, 2023, 12:55:43 am »
Hi, @c4p, @jmpessoa!

I would like to explore another option which seems to be simpler and does not require using more components (like DownloadService, etc.)

Would it be possible to simply copy the downloaded file (in the main storage device 'Downloads' folder) to a 'public', temporary directory (other than that of the app itself)?? The copied file could be opened and then deleted when the app closes - but the original download file would be preserved in the Downloads folder in case the user would like to keep it for later uses.

If this is possible, what folder should be used for this trick?

With best regards,
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 2.0.12 - FPC 3.2.0 on GNU/Linux Mint 19.1, Lubuntu 18.04, Windows XP SP3, Windows 7 Professional, Windows 10 Home

jmpessoa

  • Hero Member
  • *****
  • Posts: 2317
Re: How to download a file from web page displayed by jWebView
« Reply #43 on: June 06, 2023, 05:19:04 am »

hi, maurobio

just a side note:  do you need save the file? or only open/read  it?
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

maurobio

  • Hero Member
  • *****
  • Posts: 623
  • Ecology is everything.
    • GitHub
Re: How to download a file from web page displayed by jWebView
« Reply #44 on: June 06, 2023, 01:16:02 pm »
Hi, @jmpessoa!

Quote
just a side note:  do you need save the file? or only open/read  it?

I want both. When a user visits a website and clicks on a linked file, her/he expects to have the file opened in the associated app and to have it saved in the Downloads directory. I want that my app offers the user that very same experience.

Best regards,
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 2.0.12 - FPC 3.2.0 on GNU/Linux Mint 19.1, Lubuntu 18.04, Windows XP SP3, Windows 7 Professional, Windows 10 Home

 

TinyPortal © 2005-2018