因此,我尝试创建一个函数来下载具有可变地址的文件,具体取决于用户,该用户使用URLDownloadToFile()
输入字符串,但是实际上并没有将文件下载到磁盘上。它会跳过S_OK
,E_OUTOFMEMORY
和INET_E_DOWNLOAD_FAILURE
到UNKNOWN_ERROR
。使用GetLastError()
返回2
,在线检查表示"Not Found"
。我不确定这是否是我正在使用的网站(这是我自己的服务器,使用res.download(path,filename)
的Node.JS)。希望获得帮助,代码如下。
仅需注意,所有内容均根据需要正确包含,并且该过程(安全地)以SE_DEBUG_NAME
运行,因此希望它不会是权限错误。
// Convert String To Wide String
wstring S2WS(const string& str) {
int length;
int slength = (int)str.length() + 1;
length = MultiByteToWideChar(CP_ACP,str.c_str(),slength,0);
wchar_t* buffer = new wchar_t[length];
MultiByteToWideChar(CP_ACP,buffer,length);
wstring out(buffer);
delete[] buffer;
return out;
}
// Download File
int download(string url) {
wstring wUrl = S2WS(url);
lpcwstr lUrl = wUrl.c_str();
TCHAR path[MAX_PATH];
getcurrentDirectory(MAX_PATH,path);
HRESULT response = URLDownloadToFile(NULL,lUrl,path,NULL);
if (response == S_OK) {
cout << "S_OK" << endl;
} else if (response == E_OUTOFMEMORY) {
cout << "E_OUTOFMEMORY" << endl;
} else if (response == INET_E_DOWNLOAD_FAILURE) {
cout << "INET_E_DOWNLOAD_FAILURE" << endl;
} else {
cout << "UNKNOWN_ERROR" << endl;
}
return 0;
}
// Example Usage
int main() {
download("http://somewebsite.com/files/textfile.txt");
}