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();
Developer technologies | C++
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 122.8K 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 11,356 Reputation points Microsoft External Staff
    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.