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

Discussion in 'iOS Questions' started by tiagovsilva, Nov 7, 2018 at 2:29 PM.

  1. tiagovsilva

    tiagovsilva Member Licensed User

    I followed this thread
    https://www.b4x.com/android/forum/t...n-webview-and-how-to-download-pdf-file.66546/

    And my code is:
    Code:
    Sub WebView1_OverrideUrl (Url As StringAs Boolean
        
    If Url.EndsWith(".pdf"Then
            
    Dim j As HttpJob
            j.Initialize(
    "pdf", Me)
            j.Download(Url)
            
    Return True
        
    Else
            
    Return False
        
    End If
    End Sub
    but does nothing when trying to download.
    Is this no longer supported or am I doing something wrong?

    Thank you!
     
  2. Erel

    Erel Administrator Staff Member Licensed User

  3. tiagovsilva

    tiagovsilva Member Licensed User

    I have this now
    Code:
    Sub JobDone (Job As HttpJob)
    End Sub

    Sub WebView1_OverrideUrl (Url As StringAs 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: Nov 8, 2018 at 11:01 AM
  4. Erel

    Erel Administrator Staff Member Licensed User

    You should use iHttpUtils2 library.

    Put a breakpoint in Download sub. Is it reached?
     
    tiagovsilva likes this.
  5. tiagovsilva

    tiagovsilva Member Licensed User

    It reaches the Download Sub and the success MsgBox shows up but the PDF file is not stored on the ipad.
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    tiagovsilva likes this.
  7. tiagovsilva

    tiagovsilva Member Licensed User

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

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

    Sub WebView1_OverrideUrl (Url As StringAs 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
    Code:
    #PlistExtra: <key>UIFileSharingEnabled</key><true/>
    I know that it reaches the success MsgBox but nothing more happens.

    Again, sorry for bothering you
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    What is the output of:
    Code:
    Log("Size: " & File.Size(File.DirDocuments,"ficheiro.pdf"))
     
  9. tiagovsilva

    tiagovsilva Member Licensed User

    The output is: 56074

    But I can not find the file on the Ipad. Should it be in the iBook or in the Adobe Acrobat?
     
  10. aeric

    aeric Active Member Licensed User

    When you use Resumable Sub, you don't need this sub:
    Code:
    Sub JobDone (Job As HttpJob)
    End Sub
     
    tiagovsilva likes this.
  11. Erel

    Erel Administrator Staff Member Licensed User

    No. It is stored in an app specific folder.
     
    tiagovsilva likes this.
  12. tiagovsilva

    tiagovsilva Member Licensed User

    Is it possible to save to another folder? To be able to view in one of the mentioned apps.
     
  13. DonManfred

    DonManfred Expert Licensed User

    Sure, adapt the path

    Code:
    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
     
    tiagovsilva likes this.
  14. tiagovsilva

    tiagovsilva Member Licensed 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.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice