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>
You try to upload a file and JS Code a the same time during your SUBMIT action.
so your &js< ...> lands every 32000 characters inside your file.
&js<..> is just a hidden WRITE and allows javascript syntax checking inside the <.. >
example:
is identic to
So your concept doesn't work that way
You would require a second independent JS routine in a browser to call for progress using CSP hyperevent.
Why you don't call a method and into this method you show the message with &JS<>, you was try this?
Hi David
I think this is a feature(?) of the way Javascript and the upload work in any browser - running JS or an upload blocks any UI updating
so it's not possible to do easily or at all - I think
Peter
Try the following example of uploading multiple files at once, showing the progress for each file you upload (without form, submit, iframe, jQuery, flash, java, reloading/redrawing the page):