Send a file to a browser

Are you using LiveCode to create server scripts or CGIs?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
edgore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 197
Joined: Wed Jun 14, 2006 8:40 pm

Send a file to a browser

Post by edgore » Tue Oct 08, 2013 9:47 pm

I am trying to figure out the Livecode equivalent of this PHP:

<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');

// The PDF source is in original.pdf
readfile('original.pdf');
?>

I have everything figured out except what the livecode equivalent of the read statement is.

Basically I have a server script that accepts an upload from the user, does a bunch of processing, then creates, on the server, a zip file with three items in it. at the end of the processing I want the server to send that zip file to the user's browser for the user to download.

Previously, the result of the processing was just a csv file, which is sent to the browser with

put header "Content-dispostion: attachment; filename=mycsvfile.csv"
put header "content-Type: text/plain"
put mycsv ---a variable containing the csv data as text

But, because of changes I made to the application I now have three related files that I want to return and putting them in a zip file made the most sense, esp. since one of them is a 30 meg text file, and sending that.

I am sure that what I want to do is pretty simple, I just can't seem to figure out the right livecode to do it, and the closest thing I could find was the PHP above.

I tried using

put url "binfile:" & thePathToMyZipFile

in place of the readfile statement, but no luck. I do get a downloaded file, but it's far to small, and it's an unreadable zip file.

The code I tried was:

put header "Content-dispostion: attachment; filename=" & theNameofMyZipFile --this is just the name and extension, not the full path
put header "content-Type: application/plain"
put url "binfile:" & thePathToMyZipFile --this is the relative path & filename i.e. ziptemp/myzipfile.zip

I am sure that what I want is something along these lines - I am probably doing something dumb regarding encoding or something....

edit - oh, and I have verified that the zip file is actually being created on the server, and it is, and it's a valid zip file with the information I want in it.

Thanks!
Last edited by edgore on Wed Oct 09, 2013 3:38 am, edited 1 time in total.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Send a file to a browser

Post by Simon » Wed Oct 09, 2013 12:18 am

Hi edgore,
Is bringing the file into a browser what you really want to do or just download the file onto the machine for further processing?
Just asking because I didn't see anything that suggested the browser did anything with the download.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

edgore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 197
Joined: Wed Jun 14, 2006 8:40 pm

Re: Send a file to a browser

Post by edgore » Wed Oct 09, 2013 1:52 am

Because I am using the content type of attachment the browser asks the user to save the file - basically the end result of the server side processing is a file the user downloads.

That part is working correctly - it's just that what is getting downloaded is not a proper zip file, so I assume I am supposed to do something other than just put the binary file out to the browser...
Last edited by edgore on Wed Oct 09, 2013 3:37 am, edited 2 times in total.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Send a file to a browser

Post by Simon » Wed Oct 09, 2013 2:14 am

Hi,
You didn't actually answer what I asked.
It's really easy to just use:

Code: Select all

put url "http://www.mysite.com/download.zip into url("binfile:" & specialFolderPath("documents") & "/download.zip")
Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

edgore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 197
Joined: Wed Jun 14, 2006 8:40 pm

Re: Send a file to a browser

Post by edgore » Wed Oct 09, 2013 3:22 am

I think we are talking about different things - this isn't running on a desktop client. The script is running on the server and the server is sending the file to a browser client via http. That is what I was describing in my previous post, though maybe not too clearly.

icouto
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 92
Joined: Wed May 29, 2013 1:54 am
Location: Sydney, Australia

Re: Send a file to a browser

Post by icouto » Wed Oct 09, 2013 3:16 pm

LiveCode Server has a series of special put commands which you can use when you want to manually override what the server is sending back to the client - like in this case. These commands include things like put header, put markup and put unicode, as well as put binary.

You may want to have a look at the put binary command - and try using that rather than the plain put.

I hope this helps!

edgore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 197
Joined: Wed Jun 14, 2006 8:40 pm

Re: Send a file to a browser

Post by edgore » Wed Oct 09, 2013 3:44 pm

Thanks for the suggestion. I tried the following:

put header "Content-dispostion: attachment; filename=" & theNameofMyZipFile --this is just the name and extension, not the full path
put header "content-Type: application/zip"
put binary url "binfile:" & thePathToMyZipFile --this is the relative path & filename i.e. ziptemp/myzipfile.zip

and also with a different content type:

put header "Content-dispostion: attachment; filename=" & theNameofMyZipFile --this is just the name and extension, not the full path
put header "content-Type: application/octet-stream"
put binary url "binfile:" & thePathToMyZipFile --this is the relative path & filename i.e. ziptemp/myzipfile.zip

But I am getting the same result. If it helps any the file that is getting downloaded is 1K in size, while the original file on the server is 97K

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Send a file to a browser

Post by SparkOut » Wed Oct 09, 2013 3:56 pm

Not tested, but what about

Code: Select all

put url("binfile:" & thePathToMyZipFile) into tBinaryData
put binary tBinaryData 
?

Also, what about making the path relative to the server root, ie like "/home/myuserid/public_html/ziptemp/" & myzipfilename? Not at all certain whether that will help in any way, but when I was doing cgi scripting in the old 3.5 engine, it sometimes needed a full path.

edgore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 197
Joined: Wed Jun 14, 2006 8:40 pm

Re: Send a file to a browser

Post by edgore » Wed Oct 09, 2013 4:05 pm

That did it! I had just noticed that if I changed the extension on the downloaded files to txt and opened them that instead of binary gobbledygook they contained the filepath, in plain text, so obviously there was something going wrong with trying to directly put the url to stdout. This method of popping it into a variable first though seems to work just fine.

It seems to work just fine with relative paths, also.

For posterity and anyone else trying to do this, here is the code that worked in the end:

Code: Select all

put header "Content-disposition: attachment; filename=" & theNameofMyFile --name of the file as it will appear to the downloader
put header "Content-Type: application/zip"  --this will vary depending on the type of file you are sending
put header "content-transfer-encoding: binary"  --I don't know if this is required, but it worked, so I am not changing it

put url("binfile:" & thePathtoMyFile) into tBinaryData --thePathtoMyFile is the full path, including the filename of the file you want to send

put binary tBinaryData

Post Reply

Return to “CGIs and the Server”