iOS Question How do I download a PDF file from Webview?

tiagovsilva

Member
Licensed User
Longtime User
I have this now
B4X:
Sub JobDone (Job As HttpJob)
End Sub

Sub WebView1_OverrideUrl (Url As String) As Boolean
    If Url.EndsWith(".docx") Then
        App.OpenURLAsync(Url)
        'Msgbox("Download Completo", "Educa4YOU")
        Return True
    Else If Url.EndsWith(".pdf") Then
        Download(Url)
        Return True
    Else
        Return False
    End If
End Sub

Sub Download (link As String)
    Dim j As HttpJob
    j.Initialize("pdf", Me)
    j.Download(link)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Msgbox("ok", "Sucesso!")
    Else
        Msgbox("ok", "Erro")
    End If
    j.Release
End Sub
and when I open the PDF link, it does not download.

I dont know if this is important but I'm using HttpUtils2Service Module.
 
Last edited:
Upvote 0

tiagovsilva

Member
Licensed User
Longtime User
I'm sorry for bothering again but the file is not being saved on the ipad.

My code is now like this
B4X:
Sub JobDone (Job As HttpJob)
End Sub

Sub WebView1_OverrideUrl (Url As String) As Boolean
    If Url.EndsWith(".docx") Then
        App.OpenURLAsync(Url)
        Return True
    Else If Url.EndsWith(".pdf") Then
        DownloadFile(Url)
        Return True
    Else
        Return False
    End If
End Sub

Sub DownloadFile (link As String)
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(link)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Dim out As OutputStream = File.OpenOutput(File.DirDocuments,"ficheiro.pdf", False)
        File.Copy2(j.GetInputStream, out)
        out.Close
        Msgbox("Download efetuado com sucesso!", "Sucesso")
    Else
        Msgbox(j.ErrorMessage, "Erro")
    End If
    j.Release
End Sub

And I even added to the Project Attributes
B4X:
#PlistExtra: <key>UIFileSharingEnabled</key><true/>

I know that it reaches the success MsgBox but nothing more happens.

Again, sorry for bothering you
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Is it possible to save to another folder?
Sure, adapt the path

B4X:
Sub DownloadFile (link As String)
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(link)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Dim out As OutputStream = File.OpenOutput(File.DirDocuments,"ficheiro.pdf", False)
        File.Copy2(j.GetInputStream, out)
        out.Close
        Msgbox("Download efetuado com sucesso!", "Sucesso")
    Else
        Msgbox(j.ErrorMessage, "Erro")
    End If
    j.Release
End Sub
 
Upvote 0

tiagovsilva

Member
Licensed User
Longtime User
I read in the stackoverflow that it is not possible to access the folder of another app.
What I want is to do the same thing the browser does when you want to save a file. It gives the different options to save and read the file.

I know that didn't mention it before but I thought it was a system default.
 
Upvote 0

tiagovsilva

Member
Licensed User
Longtime User
Open local files with external apps
It works perfectly!

B4X:
Sub WebView1_OverrideUrl (Url As String) As Boolean
    If Url.EndsWith(".docx") Then
        DownloadFile(Url,"ficheiro.docx")
        Return True
    Else If Url.EndsWith(".pdf") Then
        DownloadFile(Url,"ficheiro.pdf")
        Return True
    Else
        Return False
    End If
End Sub

Sub DownloadFile (link As String, FileName As String)
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(link)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Dim out As OutputStream = File.OpenOutput(File.DirDocuments,FileName, False)
        File.Copy2(j.GetInputStream, out)
        out.Close
        di.Initialize("di", File.DirDocuments, FileName)
        di.PreviewFile(Page1)
    Else
        Msgbox(j.ErrorMessage, "Erro")
    End If
    j.Release
End Sub

Thanks Erel!
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
It works perfectly!

B4X:
Sub WebView1_OverrideUrl (Url As String) As Boolean
    If Url.EndsWith(".docx") Then
        DownloadFile(Url,"ficheiro.docx")
        Return True
    Else If Url.EndsWith(".pdf") Then
        DownloadFile(Url,"ficheiro.pdf")
        Return True
    Else
        Return False
    End If
End Sub

Sub DownloadFile (link As String, FileName As String)
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(link)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Dim out As OutputStream = File.OpenOutput(File.DirDocuments,FileName, False)
        File.Copy2(j.GetInputStream, out)
        out.Close
        di.Initialize("di", File.DirDocuments, FileName)
        di.PreviewFile(Page1)
    Else
        Msgbox(j.ErrorMessage, "Erro")
    End If
    j.Release
End Sub

Thanks Erel!
what is di here ?
 
Upvote 0
Top