• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 493
  • Last Modified:

Prompted File Download Fails in IE

Hello,

I have 2 problems:

1. File download fails in all IE's.

I have written a CGI page that will allow a user to download the
contents of a file.  But when the page loads, prompts the user to
save, an error message will appear.

This code works fine in all Firefox's, but I have searched through
many forums and have yet to get a solution for this problem.

2. File name of download is incorrect.

Ideally I pass in information to the CGI file with the information
about the file to download using the Query string.  When I do this
the download dialog box shows part of the Query string as the
file name instead of that specified in the Content-disposition.

And even though if I hard code file information into the CGI, thus
leaving the Query string blank, the name of the CGI file is used
instead of the Content-disposition filename.

I have attached a screenshot of the error and a copy of the
CGI code.
#define SYM_CACHE_CTRL      "Cache-Control"
#define SYM_NO_CACHE        "no-cache"
#define SYM_REVALIDATE      "must-revalidate"
#define SYM_POST_CHK        "post-check"
#define SYM_PRE_CHK         "pre-check"
#define SYM_PRAG_PUBLIC     "Pragma: Public"
#define SYM_PRAG_PRIVATE    "Pragma: Private"
#define SYM_CONT_TYPE       "Content-Type"
#define SYM_CONT_LEN        "Content-Length"
#define SYM_CONT_DISP       "Content-Disposition"
#define SYM_ATTACH          "attachment"
#define SYM_FILENAME        "filename"
#define SYM_XCONT_TYPE      "X-Content-Type-Options"
#define SYM_NO_SNIFF        "nosniff"

#define MIME_HEADER         "MIME-Version 1.0"

#define TYPE_TXT_PLAIN      "text/plain"

#define MAX_FILE_SIZE       50
#define MAX_PATH_SIZE       (200 + MAX_FILE_SIZE)
#define MAX_TEXT_SIZE       100


SINT32 main(SINT32 argc, SINT8 *argv[])
{
    UINT32 size;
    FILE *file = NULL;
    SINT8 fileName[MAX_FILE_SIZE], 
          fileSrc[MAX_PATH_SIZE], 
          fileType[MAX_TEXT_SIZE], 
          output[MAX_TEXT_SIZE];

    // File type, name, source
/*    if(wbm_lib_param_get(WBM_PARAM_TYPE, fileType) == FAIL || 
       wbm_lib_param_get(WBM_PARAM_NAME, fileName) == FAIL || 
       wbm_lib_param_get(WBM_PARAM_SRC, fileSrc) == FAIL)
    {
        return FAIL;
    }
*/
sprintf(fileSrc, "%s", "/var/config/config.conf");
sprintf(fileType, "%s", "text/plain");
sprintf(fileName, "%s", "Config.conf");
    // Open file
    if((file = fopen(fileSrc, "r")) == NULL)
    {
        return FAIL;
    }

    fseek(file, 0, SEEK_END);
    size = ftell(file);
    rewind(file);

    // MIME-Version 1.0
//    printf("%s\n", MIME_HEADER);

    // Cache-Control: must-revalidate, post-check=0, pre-check=0
//    printf("%s: %s\n", SYM_CACHE_CTRL, SYM_NO_CACHE);
//    printf("%s: %s, %s=0, %s=0\n", 
//           SYM_CACHE_CTRL, SYM_REVALIDATE, SYM_POST_CHK, SYM_PRE_CHK);

    // Pragma: Public
//    printf("%s\n", SYM_PRAG_PUBLIC);

    // X-Content-Type-Options: nosniff
//    printf("%s: %s\n", SYM_XCONT_TYPE, SYM_NO_SNIFF);
    
    // Content-disposition: attachment; filename=NAME;
    printf("%s: %s; %s=\"%s\";\n", 
           SYM_CONT_DISP, SYM_ATTACH, SYM_FILENAME, fileName);

    // Content-type: TYPE
    printf("%s: %s\n", SYM_CONT_TYPE, fileType);

    // Content-length: LENGTH
    printf("%s: %u\n", SYM_CONT_LEN, size);

    printf("\n");

    // Print contents of file
    while(fgets(output, sizeof(output), file) != NULL)
    {
        printf("%s", output);
    }

    if(file != NULL)
    {
        fclose(file);
    }

    return 0;
}

Open in new window

Download-Error.PNG
0
sctccomm
Asked:
sctccomm
  • 2
1 Solution
 
sctccommAuthor Commented:
Found out that Internet Explorer has quite a bit of problems supporting "Content-disposition".

For "Content-Disposition: attachment, ...", all IE's seem to have trouble when you open this kind of page in a new window or tab.  Thus when creating a page, forcing a file download must be done within the same in window.

Or I believe using <href> should always work.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now