How get internet filename

drjackool 956 Reputation points
2021-08-17T05:51:14.887+00:00

Hi
I using following code to get a file information from url, but GetFileName() method not return pure filename! it just returns part of url including trailing numbers!

CInternetSession sess;
CHttpFile* pfile = (CHttpFile*)sess.OpenURL(_T("https://dl2.soft98.ir/soft/s/Solid.PDF.Tools.10.1.12248.5132.rar?1629176293"), 1, INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_RELOAD);

        if (pfile)
        {
            TRACE0("ok\n");
            TRACE1("Filename: %s\n", pfile->GetFileName()); // output is: Solid.PDF.Tools.10.1.12248.5132.rar?1629176293

            DWORD dw;
            pfile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH, dw);
            TRACE1("File lenght: %u\n", dw);

            pfile->Close();
            delete pfile;
        }

        sess.Close();
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,568 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 113.2K Reputation points
    2021-08-17T07:03:46.347+00:00

    I think that you can exclude it:

    CString example = _T("Solid.PDF.Tools.10.1.12248.5132.rar?1629176293");
    int i = example.FindOneOf( _T("?#") );
    CString filename = i >= 0 ? example.Left( i ) : example;
    

  2. Jeanine Zhang-MSFT 9,266 Reputation points Microsoft Vendor
    2021-08-19T08:18:57.987+00:00

    According to your description, I wonder if you want to get the filename from the URL and the filename is not contained in the URL? If so, you couldn't get internet filename via GetFileName() method

    As far as I'm concerned you could inspect the Content-Disposition response header using an HTTP request to get the filename.