Partager via


interface ICoreWebView2File

Note

This reference is no longer being maintained. For the latest API reference, see WebView2 API Reference.

interface ICoreWebView2File
  : public IUnknown

Representation of a DOM File object passed via WebMessage.

Summary

Members Descriptions
get_Path Get the absolute file path.

You can use this object to obtain the path of a File dropped on WebView2.

    CHECK_FAILURE(m_webView->add_WebMessageReceived(
        Callback<ICoreWebView2WebMessageReceivedEventHandler>(
            [this](ICoreWebView2* sender, ICoreWebView2WebMessageReceivedEventArgs* args)
            {
                wil::com_ptr<ICoreWebView2WebMessageReceivedEventArgs2> args2 =
                    wil::com_ptr<ICoreWebView2WebMessageReceivedEventArgs>(args)
                        .query<ICoreWebView2WebMessageReceivedEventArgs2>();
                wil::com_ptr<ICoreWebView2ObjectCollectionView> objectsCollection;
                args2->get_AdditionalObjects(&objectsCollection);
                unsigned int length;
                objectsCollection->get_Count(&length);

                // Array of file paths to be sent back to the webview as JSON
                std::wstring pathObjects = L"[";
                for (unsigned int i = 0; i < length; i++)
                {
                    wil::com_ptr<IUnknown> object;
                    objectsCollection->GetValueAtIndex(i, &object);

                    wil::com_ptr<ICoreWebView2File> file = object.query<ICoreWebView2File>();
                    if (file)
                    {
                        // Add the file to message to be sent back to webview
                        wil::unique_cotaskmem_string path;
                        file->get_Path(&path);
                        std::wstring pathObject =
                            L"{\"path\":\"" + std::wstring(path.get()) + L"\"}";
                        // Escape backslashes
                        std::wstring pathObjectEscaped;
                        for (const auto& c : pathObject)
                        {
                            if (c == L'\\')
                            {
                                pathObjectEscaped += L"\\\\";
                            }
                            else
                            {
                                pathObjectEscaped += c;
                            }
                        }
                        pathObjects += pathObjectEscaped;

                        if (i < length - 1)
                        {
                            pathObjects += L",";
                        }
                    }
                }
                pathObjects += L"]";

                // Post the message back to the webview so path is accessible to content
                m_webView->PostWebMessageAsJson(pathObjects.c_str());

                return S_OK;
            })
            .Get(),
        &m_webMessageReceivedToken));

Applies to

Product Introduced
WebView2 Win32 1.0.1774.30
WebView2 Win32 Prerelease 1.0.1777

Members

get_Path

Get the absolute file path.

public HRESULT get_Path(LPWSTR * value)

The caller must free the returned string with CoTaskMemFree. See API Conventions.