B4J Question WebApp - WebSocket - Download mp3 directly and not stream

hatzisn

Well-Known Member
Licensed User
Longtime User
Hello, I tried to do what I mentioned in the title with the following code and I cannot. When I tried the code in the following thread a new window opens and tries to stream the mp3. Any suggestions?

 

Magma

Expert
Licensed User
Longtime User
Hello, I tried to do what I mentioned in the title with the following code and I cannot. When I tried the code in the following thread a new window opens and tries to stream the mp3. Any suggestions?

I think it is because of browser setting... you mean to "make it" just to download it...
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
I found a way in StackOverflow:


Here is the modified code :

Java script code:
    function downloadfile(dnldfilename) {
   
        var link = document.createElement('a');
        link.href = dnldfilename;
        link.setAttribute('download', dnldfilename);
        document.getElementsByTagName("body")[0].appendChild(link);
        // Firefox
        if (document.createEvent) {
            var event = document.createEvent("MouseEvents");
            event.initEvent("click", true, true);
            link.dispatchEvent(event);
        }
        // IE
        else if (link.click) {
            link.click();
        }
        link.parentNode.removeChild(link);
       
    }

and then we run this in B4J

B4X:
ws.RunMethod("downloadfile", Array("mymp3file.mp3"))
 
Last edited:
Upvote 0

Johan Hormaza

Well-Known Member
Licensed User
Longtime User
B4X:
'Handler class Downloader
Sub Class_Globals
    
End Sub

Public Sub Initialize

End Sub

Sub Handle(req As ServletRequest, resp As ServletResponse)
    Dim Filename As String = req.GetParameter("file")
    If File.Exists("D:\LuzMSA\audios",Filename) Then
        Dim in As InputStream = File.OpenInput("D:\LuzMSA\audios",Filename)
        File.Copy2(in,resp.OutputStream)
        Log("Descargar: " & Filename)
    Else
        Dim in As InputStream = File.OpenInput(File.DirAssets,"NO.png")
        File.Copy2(in,resp.OutputStream)
        Log("No existe este archivo: " & Filename)'Firma123456789123456789.png
    End If
End Sub
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
B4X:
'Handler class Downloader
Sub Class_Globals
   
End Sub

Public Sub Initialize

End Sub

Sub Handle(req As ServletRequest, resp As ServletResponse)
    Dim Filename As String = req.GetParameter("file")
    If File.Exists("D:\LuzMSA\audios",Filename) Then
        Dim in As InputStream = File.OpenInput("D:\LuzMSA\audios",Filename)
        File.Copy2(in,resp.OutputStream)
        Log("Descargar: " & Filename)
    Else
        Dim in As InputStream = File.OpenInput(File.DirAssets,"NO.png")
        File.Copy2(in,resp.OutputStream)
        Log("No existe este archivo: " & Filename)'Firma123456789123456789.png
    End If
End Sub

Thank you, very useful. Mine though was kind of a different problem. In a websocket application there is no response object to copy the stream of the file. Nevertheless it is a clever way to do this and searching the web in the morning I found that adding contenttype=application/octet-stream and the header content-disposition =attachment;filename:heregoesthefilename can be saved as a downloaded file. Using only the prementioned contenttype it only writes the bytes to the stream which then you reassemble in the receiving part.
 
Upvote 0
Top