Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SendFile inside a TIWCallbackProc3
#1
How can I have a WebApplication.SendFile(..) response into the TIWCallbackProc3 = reference to procedure (aParams: TStrings; out aResult: string; out aHandled: Boolean) callback?

I test for the request.
In some cases, I only have to assign aResult.
But in others, I have to run a download attachment.
The IW command is WebApplication.SendFile() / SendStream().
I assigned aHandled := true but I get "Uninitialized response" and no download starts.
So I added WebApplication.Response.Code := 200 so the error messages do not appears anymore, but no download starts.
Thank you.
Reply
#2
An Async request initiated by IntraWeb JavaScript code expects to receive a XML response, formatted in a very specific way.

So in theory you can't send a file in an async reponse.

Having said that, it is possible to encode a file as, for instance, a base64 string and send it back as part of your XML packet. Then the calling code should be able to take the response and decode it back. But then you will hit another wall, which is the browser security that won't let your JS code do anything useful with this file (i.e. you won't be able to save it to the user local disk).

If you want to download a file, use one of the well known ways to start a file download and you should be fine.
Reply
#3
Thank you Alexandre.
So I solved with a differente approach: into the async response provided by IW I send a message like "download at this URL" and the client-side JS script execute samething like this:

Code:
var w = window.open();
if(!w || w.closed || typeof w.closed=='undefined') {
    toastr("info","File",clkLang(["Controllare eventuali blocchi di popup del browser","Please check if browser locked popup"]));
};
w.location = r.hRef;
w.focus();

Server side, I used a cache file URL for browser managed content (i.e. PDF), or a TContentBase descendant custom URL on order to respond with a WebApplication.SendFile(FileName,true) for other files type (i.e. .p7m, client side file download).
  
But also I have also this older code working:

Client side:
Code:
executeAjaxEvent(`&mode=${mode}&k=${k}&fn=${fn}&op=open`, null, "global.infowebDocOpen", false, null, false);

Server side I've registered the "global.infowebDocOpen" on a procedure like this:
Code:
... prepare the file ...
WebApplication.SendFile(FileName, false, '', '')

And it works even better than my code upper: PDF file is opened in a new browser tab, and for non managed file extension (like a .doc) a download starts.

Since executeAjaxEvent() is also an asynchronous event handling of IntraWeb, I thought it worked similarly in calling ajaxCall() which looks similar and simplifies response handling.

At this point I could use a second approach, in which the use of ajaxCall() is abandoned, instead returning to the previous executeAjaxEvent() which instead supports SendFile, and simplifies the entire call-and-response process for opening or downloading files, without the need to use two steps above.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)