?
Solved

Prompted File Download Fails in IE

Posted on 2011-10-17
2
Medium Priority
?
474 Views
Last Modified: 2013-12-25
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
Comment
Question by:sctccomm
  • 2
2 Comments
 

Accepted Solution

by:
sctccomm earned 0 total points
ID: 36988392
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question