CWebView::SetDownloadPath Method  
 
BOOL SetDownloadPath(
  DWORD dwFileId,  
  LPCTSTR lpszFilePath  
);

The SetDownloadPath method changes the location of the local file for the specified download.

Parameters

dwFileId
An unsigned integer value which uniquely identifies the download. This value cannot be zero.
lpszFilePath
A null-terminated string which specifies the new path for the downloaded file. This value may be a file:// URL or it may specify a local file path. If the path contains environment variables surrounded with percent (%) symbols, the variables will be expanded. This parameter cannot be NULL or an empty string.

Return Value

If the method succeeds, the return value is non-zero. If the method fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

The specified path may be provided as either a file:// URL or a local file system path. If a file:// URL is given, it will be converted to a local path before use. Environment variables enclosed in percent signs (such as %TEMP%) are expanded automatically. If the path refers to an existing directory rather than a file name, the original file's base name is preserved and the file will be created in that directory. This allows applications to easily redirect a download into a specific folder without changing the file name.

The method always normalizes the provided path. If a relative path is specified, it is resolved against the current working directory of the process. Because this can lead to unpredictable results in multithreaded applications, it is strongly recommended that applications always provide absolute paths. By default, downloaded files are typically stored in the current user's Downloads folder.

Several restrictions are enforced on the path to help ensure safety and compatibility. Device names such as CON, PRN, or NUL are not permitted. Downloads cannot be redirected into system folders such as the Windows folder or Program File folder. The path cannot contain reserved characters, and files that are marked as read-only will not be overwritten. If any of these conditions are detected, the method will fail with an error.

This method can only be called within a handler for the WEBVIEW_EVENT_DOWNLOAD_STARTING event. Once the download has begun, the local file path cannot be changed. If you wish to move the file after it has been downloaded, you can use the GetDownloadPath method to obtain its current location and then use a Win32 function like MoveFileEx to move the file to a new folder.

Note that you should avoid showing modal dialogs such as the common "Save File" dialog within event handlers. Because the browser events are raised within the context of the WebView2 UI thread, displaying a dialog asking the user for a new download location can prevent the control from processing its internal message loop, potentially leading to stalled downloads.

Requirements

Minimum Desktop Platform: Windows 7 Service Pack 1
Minimum Server Platform: Windows Server 2008 R2 Service Pack 1
Header File: cstools12.h
Import Library: cshtmv12.lib
Unicode: Implemented as Unicode and ANSI versions

See Also