NetTalk Central

Author Topic: Open a file in client's browser?  (Read 5495 times)

Simon Kemp

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Open a file in client's browser?
« on: November 19, 2007, 01:40:30 AM »
Me newbing again.

Could someone briefly explain the steps to send a file (it's a spreadsheet) to client browser? I see example 14 pushing a pdf but can't see how/why it works. It also seems the file is deleted after it's sent - is that correct and what triggers it?

Also, is it possible to do more of a filedialog/save kind of thing?

Many thanks,

Simon

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 10824
    • View Profile
Re: Open a file in client's browser?
« Reply #1 on: November 19, 2007, 07:11:28 AM »
>> Could someone briefly explain the steps to send a file (it's a spreadsheet) to client browser?

The browser asks for something. Always, the browser is the one doing the asking.
A spreadsheet is just a file, so if the browsers asks for the file, you can send it the file.
Typically asking for the file is as simple as clicking on a URL.

>> I see example 14 pushing a pdf but can't see how/why it works.

magic...

The browser requests MailBoxesReport. It has no idea what this is, or what it's going to get, but that's what it's told to ask for.

This passes through the WebHandler procedure, which directs it to the MailBoxesReport Procedure. In there a report is generated, and converted to PDF. The pdf file is written to disk, and the sent back to the browser (using p_web._sendfile).
Noe that the browser asked for a file, and it got one. That's all that matters.
The Content-Type of the file being sent back is set to 'application/pdf'. By doing this the browser can decide whether the file can be opened (which it will always try to do) or whether it should ask the suer if they want to save it.

>>  It also seems the file is deleted after it's sent - is that correct and what triggers it?

yes that is correct. Files starting with $$$ in the name are designed to be "serve once" files and are deleted after they're sent. This is useful whenever you are building a "temporary" file to be sent to the user.

>> Also, is it possible to do more of a filedialog/save kind of thing?

yes. By altering the content-type to something the browser doesn't recognize. But I think I need to write up a separate article for that.

Cheers
Bruce

Simon Kemp

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: Open a file in client's browser?
« Reply #2 on: November 20, 2007, 01:11:50 AM »
Hi Bruce,

That really helped, thank-you (in fact a fair bit was in the FAQs, apologies). Three more questions I'd appreciate anyone's help with:

1. What types of procedures are automatically handled by the WebHandler? It looks like NetWebForm, NetWebPage etc always get in there but I don't understand how in example 14 'MailboxesReportWithOptions' (a Report) gets into the list. In particular, I wanted to use a simple source procedure, prototyped (NetWebServerWorker p_web) and containing the magic p_web._Sendfile(). I ended up putting some handcode into theWebHandler so it would "serve the procedure". Must be the wrong way?

2. How do I put some simple text on a tab on a NetWebForm (some "notes" for the user)?

3. When using p_web._Sendfile() is there a way to override the url being used as the filename? What I mean is my excel file always opens as "StockListToExcel.xls" based on the server procedure name - I'd like to change it to "Client Name dd_mm_yyyy.xls" (for example).

Thanks again for your patience!

Simon

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 10824
    • View Profile
Re: Open a file in client's browser?
« Reply #3 on: November 20, 2007, 05:17:51 AM »
Hi Simon,

>> 1. What types of procedures are automatically handled by the WebHandler? It looks like NetWebForm, NetWebPage etc always get in there but I don't understand how in example 14 'MailboxesReportWithOptions' (a Report) gets into the list.

All the "page" style procedures get here. Reports get added because of the NetTalk extension to the report. NetWebSource procedures are not called here because they're designed to be "included" on pages, not be actual pages themselves.

>> In particular, I wanted to use a simple source procedure, prototyped (NetWebServerWorker p_web) and containing the magic p_web._Sendfile(). I ended up putting some handcode into theWebHandler so it would "serve the procedure". Must be the wrong way?

Start with a NetWebPage, not a NetWebSource.
There are a few examples that show embedding in a netWebPage - and from here you can do pretty much anything you like. I think the File Download example is a good place to see this in action.

>> 2. How do I put some simple text on a tab on a NetWebForm (some "notes" for the user)?

There's a "Display" form-field type. You can display text, URL's, images, buttons and so on.

>> 3. When using p_web._Sendfile() is there a way to override the url being used as the filename? What I mean is my excel file always opens as "StockListToExcel.xls" based on the server procedure name - I'd like to change it to "Client Name dd_mm_yyyy.xls" (for example).

nope, you can't do this. The _browser_ determines the name, not the server. ie the Browser "asks" for a file, and it always does the save-as with the name it asked for. There's no way for the server to say "hey, you asked for a.xls, but actually I'm sending you b.xls instead".

cheers
Bruce

Simon Kemp

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: Open a file in client's browser?
« Reply #4 on: November 20, 2007, 06:14:05 AM »
Thanks Bruce,

Starting to make a lot more sense now.

>> nope, you can't do this. The _browser_ determines the name, not the server. ie the Browser "asks" for a file, and it always does the save-as with the name it asked for. There's no way for the server to say "hey, you asked for a.xls, but actually I'm sending you b.xls instead".

Found a slightly messy way around this. Construct the "spreadsheet" url dynamically along the lines ClientName_StockList_yyyymmdd, then in the WebHandler check for urls containing _StockList_yyyymmdd and call a common procedure. Rather pleased with myself  ;D

All the best,
Simon

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 10824
    • View Profile
Re: Open a file in client's browser?
« Reply #5 on: November 22, 2007, 12:59:08 AM »
Hi Simon,

yes, well done!

Since the name is in the URL, you can sometimes get the name "right" when the URL is created. Then handle the "complex" URL generically in the WebHandler, _SendFile method.

Not really "messy" - just right.

This doesn't always work (sometimes you don't know, and can't calculate, the correct name when the URL is created) but when it does work it's sweet!

Cheers
Bruce

Simon Kemp

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: Open a file in client's browser?
« Reply #6 on: November 22, 2007, 02:38:02 AM »
Hi Bruce,

So would this be the best way to handle something like an "unsubscribe" link in emails? Build your database sysid into a url:

http://someservername/unsubscribefromsomelist_9873546

Then call a page based on "unsubscribefromsomelist"? Maybe obfuscate the sysid along the way?

Thanks again,

Simon

Simon Kemp

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: Open a file in client's browser?
« Reply #7 on: November 22, 2007, 11:21:41 AM »
A related question:

My NetWebPage is calling a (possibly lengthy) procedure to generate the xls it will supply using _sendfile(). Lengthy enough that a browser will give up waiting.

Chris Cordes "progress page" posts in the share knowledge forum have got me started in the sense that I'm definitely 'tickling' the browser to keep it interested. BUT, because I ultimately need to just poke an xls (p_web.ReplyContentType = 'application/vnd.ms-excel' then 'p_web._Sendfile(loc:xlsname)') things aren't working too well.

At the moment, I'm 'tickling' by sending a short blank string (please don't laugh, I'm a newbie remember):-

mytickler   string(' ')
p_web.ParseHTML(mytickler, 1, size(mytickler), NET:NoHeader)

More by luck than judgment, IE seems happy with this - it waits, waits, waits and then accepts the xls correctly. Firefox is not happy, offers to save but has lost knowledge of the content type.

So (and I'm not even sure this is the right question), just how do I keep a browser happy while it's waiting to receive my damn xls?

Thanks,

Simon

 

   


     

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 10824
    • View Profile
Re: Open a file in client's browser?
« Reply #8 on: November 27, 2007, 02:04:38 AM »
Hi Simon,

First the URL question.

yes, that's one way to do it. Another is to pass the sysid as a parameter rather than as part of the actual url name.
for example
http://someservername/unsubscribefromsomelist?id=9873546

Then you'd have a function (called just unsubscribefromsomelist) and inside that function you can get the value of the parameter using
p_web.GetValue('id')
This function would need to be a "page" function - ie one that returns a page (ie not NetWebSource).

Cheers
Bruce
 

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 10824
    • View Profile
Re: Open a file in client's browser?
« Reply #9 on: November 27, 2007, 02:06:38 AM »
Now for the "tickling" question.
And the answer is, at the moment, "I'm not sure".
I haven't had much of a chance to look into this just yet.
I'll try and work up an example later this week.

Cheers
Bruce

Simon Kemp

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: Open a file in client's browser?
« Reply #10 on: November 28, 2007, 03:35:32 AM »
Hi Bruce,

Thanks for all your help so far. And a steer on "tickling" would be very much appreciated!

All the best,
Simon