in reply to CGI - Creating Multipart Form with a File Download

AFAIAA your requirement cannot be satisfied without resorting to something like AJAX. A single CGI script can either display a page or return content as a download but not both. This is because the content type is specified by the Content-Type header and this can only be given once per request. In your code above you give this twice (see the two calls of the header() method) and so the second one has little effect which is why your file contents appear in the web page.

Either resort to AJAX or consider a different way of delivering the content to the user.

  • Comment on Re: CGI - Creating Multipart Form with a File Download

Replies are listed 'Best First'.
Re^2: CGI - Creating Multipart Form with a File Download
by Anonymous Monk on Mar 13, 2015 at 22:50 UTC
    *cough* meta-refresh is plain html
Re^2: CGI - Creating Multipart Form with a File Download
by stefl (Acolyte) on Mar 14, 2015 at 13:50 UTC

    Thanks very much for your reply! This might be a stupid question but is there any way to use more than one CGI script for a form? I've tried using a redirect (instead of calling the download subroutine) but that hasn't worked, but I'm wondering if there is some way of doing it.
    Otherwise I'll start learning about AJAX as you suggested.

      Those answers from off-brand monk are bonkers. Meta refresh has nothing to do with what you want. You’re trying to do a simple download with some prep work, not watch a long running process. If I have time I’ll try to fix up something for you but I’m swamped this weekend. Ajax could make it nicer and cleaner but isn’t necessary. Two CGIs might be a good way to go as well… Your original code looks like it is pretty close to workable/working but I haven’t tested it.

        Those answers from off-brand monk are bonkers. Meta refresh has nothing to do with what you want.

        Not any more than various suggestions of AJAX ... any download initiated via AJAX can be initiated via meta-rafresh