Published on InterSystems Developer Community (https://community.intersystems.com)

Home > How to add Javascript code to csp file down load application.

Question
David Sterngast · Jun 20, 2018

How to add Javascript code to csp file down load application.

When I down load a file using a csp page, the application  shows  the percentage as  37 %, 74 %, and 100 %.    I am trying to make changes to the application that it shows one percentage after each read.  The first time it reads, it would display 37%.   The next run would replace

37 %  with 74 % and so on until it completes its download.   I have tried adding javascript code to the application  however it doesn't work.   For example when I enter &js('alert');, the application displays it value on the screen.  Also, I tryed putting it between two scripts tags and it doesn't work.

I can use html however I cannot update the line after it displays information.   Does anybody know how I can use javascript code with cache method?   

Below is my code:

 

<form enctype="multipart/form-data" method="post" action="upload20180615.csp">
    Enter a file to upload here: <input type=file size=30 name=FileStream>
    <hr />
    <ul><input type="submit" value="Upload file"></ul>
     
</form>

<csp:if condition='($data(%request.MimeData("FileStream",1)))'>
    <h2>Saving file...</h2>
    <h0 style="background-color:MediumSeaGreen;">Process status: </h0>
    
    <script language="Cache" runat="server">
        Set data=%request.MimeData("FileStream",1)
        Set stream=##class(%FileBinaryStream).%New()
        Set D=%request.MimeData("FileStream",1).FileName ;selected file
        Set Size=%request.MimeData("FileStream",1).Size 
           
        ;Set file=##class(%File).%New(D)
                
        S L="" F I=1:1:10 S T=$P(D,"\",I) I T=""  S L=I Q        
        S FILE=$P(D,"\",L-1)
                
        S DIR="F:\a misc\"
        ;
        S FILENAM=$E(FILE,1,$L(FILE)-4)
        S FILENAM=$ZSTRIP(FILENAM,"<>","W")
        
        S DIR=DIR_FILENAM
        S X=$ZF(-1,"MKDIR """_DIR_"""")
        S DIR=DIR_"\"
        ;retrieve the file and place in the new placeo
                
        Do stream.LinkToFile(DIR_FILE)
                        
        Set RTotal = 0
        While (data.AtEnd'=1) {
        Set len=32000
         Set x=data.Read(.len)
         Set RTotal=RTotal+len
         Set Percent = ((RTotal / Size) * 100) \ 1
         Do stream.Write(x)
         ;Write "<h>"_Percent_" % </h>"
          s d=Percent s:$l(d)=2 d=" "_d s d=d_" %"
           
           ;Write "<h style=background-color:rgb(60,179,113);>"_Percent_" % </h>"
           ;&js< alert('hi') >
            &html<
                >
            &html< <h>#(d)#</h> >
        }

Set Status = stream.SaveStream()
        If (Status = 1) {
            Write "<h2>Uploaded!</h2>"            
        }
        Else {
            Write "<h2>Saving failed!</h2>"
        }
             
}
        
    </script>
</csp:if>
</body>
</html>

#Object Data Model #Caché

Source URL:https://community.intersystems.com/post/how-add-javascript-code-csp-file-down-load-application