Force user download file from server

Hi all,

I like to force the user download file when the user click on the hyperlink. i find out one example “E3CONTYP” in sysweb3 which similar to what i want.
The problem is, the download file window not pop up when the content type is “text/plain”. the file content will just show in the browser instead of force user to download.
so how to force the user download every kind of file when he click on the hyperlink?
thanks.

i find out that to pop out the download file window, we need to add these header (php code):
header(“Content-Type: application/msword”);
header(“Content-Disposition: attachment; filename=file.ext”);

but i don’t know how translate these php into natural code.
as i know, natural can add CONTENT-TYPE. E.G.
PERFORM W3CONTENT-TYPE ‘application/msword’
so how about CONTENT-DISPOSITION?

:idea: All additional http headers can be set with:

PERFORM W3HTTP-HEADER 'headerName' 'headerValue'

:arrow: Your example may look like this:

OPTIONS TQMARK=OFF
#SOURCE := 'myFilename.ext'
COMPRESS  'attachment; filename="' #SOURCE '"' 
  INTO #VALUE LEAVING NO
PERFORM W3HTTP-HEADER 'Content-Disposition' #VALUE

:roll: Additionally to avoid caching, I would add:

PERFORM W3HTTP-HEADER 'Cache-Control' 'public'
PERFORM W3HTTP-HEADER 'Expires'       '0'
PERFORM W3HTTP-HEADER 'Pragma'        'public'

:wink: To determine the correct mime type for a given extension you can use:

PERFORM W3MIME-TYPE #MIME-TYPE #FILE-EXTENSION

:arrow: This call searches the mime-type for a given file extension inside the Natural text object MIMETYPE at library SYSWEB3 - may see the NAT-RES example at library SYSWEB3… and/or the documentation.

yeah… this is what i’m finding… i added it and it just work fine…
thank you very much… :D:D

Hi all,

Now, i’m facing problem that write content of the document to let the user download it. e.g. My original text file content is:
Hello world.
Testing here!

but the result by downloading it show “Hello world.#Testing here!#” (Note: # = square box(i assume this special character is line break))

1st method i try to write the content is read resource:
PERFORM W3READ-RESOURCE " " #FILE-NAME #W3VALUE
PERFORM W3TEXTLINE #W3VALUE

but the result shown no line break as the original text file. so i even try use read work file and write the content one by one line:

DEFINE WORK FILE 25 #FILE-NAME TYPE ‘UNFORMATTED’
READ WORK FILE 25 #RECORD
END-WORK
EXAMINE #RECORD FOR H’0D’ GIVING NUMBER #LINE-NUMBER

FOR #I 1 TO #LINE-NUMBER
#STARTPOS := #STARTPOS + 1
EXAMINE #RECORD FOR H’0D’ GIVING POSITION #ENDPOS
#LENGTH := #ENDPOS - #STARTPOS
MOVE SUBSTR(#RECORD,#STARTPOS,#LENGTH) TO #CONTENT
PERFORM W3TEXTLINE #CONTENT

EXAMINE #RECORD FOR H'0D'    REPLACE FIRST ' '
EXAMINE #RECORD FOR #CONTENT DELETE FIRST 

END-FOR

i still cant get the original text file content which has line break. how to solve this problem?
thanks.

is the text file being read by another program when it arrives? That is, is the text file being viewed by an end-user or by a program? If it is the end-user, simply changing the program used to view the text file can make a difference since they interpret the cr/lf combinations differently: if you are using Notepad, try Wordpad or vice-versa.

It’s more simple…

If you work with W3TEXT* - your data is may translated from the current Natural code page to the target code page your http server is running at. This is done e.g. automatically by EXX or the other available transport layers as DCOM or PAL (Spod).
Second if EXX is used, and you may use the standard translation tables and then some special character e.g. CR and LF may be replaced with BLANK.
This made it necessary to change the CR/LF to another specific string (used e.g. inside W3TEXTLINE), which is the reason that your can’t see your line breakes.

What you probably want is to transfer a file - even with binary data - to the Web Browser, without any changes.
Therefore a second way of data transport has been introduced with SYSWEB3.
Instead of using W3TEXT* - or W3HTML*, you have to use W3BINARY.
If W3BINARY is used, your data will be delivered in a different way, and your data will be transfered without any changes. This makes it possible to transfer even pictures! But don’t mix up W3BINARY and W3TEXT* or W3HTML* because the data will not be combined and always only one of the data container is transfered.

If you would have used W3LOAD-RESOURCE instead of W3READ-RESOURCE, W3LOAD-RESOURCE would have automatically saved your data as binary, if the extension you use is specified at the MIMETYPE(SYSWEB3) text object for translation of the extension into a mime type and second entry at MIMEDATA(SYSEXT3) text object can be found, which specifies which mime type contains binary data.

As [i]Weihnachtb

yes, instead of using W3TEXTLINE, use W3BINARY:

PERFORM W3READ-RESOURCE 1X #FILE-NAME #CONTENT
PERFORM W3BINARY #CONTENT

The file downloaded is just same as the file in the server. :smiley: Thanks for all u guys help…

I can generate a file from any route or only from resource Natural LIB

Someone has a complete example of the case, forgive :shock:

:roll: See documentation about workfile handling:
http://techcommunity.softwareag.com/ecosystem/documentation/natural/nat634win/sm/definewo.htm#definewo
http://techcommunity.softwareag.com/ecosystem/documentation/natural/nat634win/sm/readwork.htm
http://techcommunity.softwareag.com/ecosystem/documentation/natural/nat634win/sm/closewf.htm#closewf

:arrow: Replace from the above example the line:

PERFORM W3READ-RESOURCE 1X #FILE-NAME #CONTENT

:idea: with:

DEFINE WORK FILE 12 'C:\Program Files\Software AG\Natural\6.3\License.txt' TYPE "UNFORMATTED"  
READ WORK FILE 12 #CONTENT 
END-WORK 
CLOSE WORK 12

for a fixed file (Windows) not part of a Natural resource directory.