为什么URLDownloadToFile()无法下载我的文件?

因此,我尝试创建一个函数来下载具有可变地址的文件,具体取决于用户,该用户使用URLDownloadToFile()输入字符串,但是实际上并没有将文件下载到磁盘上。它会跳过S_OKE_OUTOFMEMORYINET_E_DOWNLOAD_FAILUREUNKNOWN_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");
}
PUXI3230 回答:为什么URLDownloadToFile()无法下载我的文件?

URLDownloadToFile中,path应该是文件路径,而不是目录路径。您可以将一些文件名附加到path变量中,它应该可以工作。

TCHAR path[MAX_PATH];
GetCurrentDirectory(MAX_PATH,path);
wcscat_s(path,L"\\filename.txt");
本文链接:https://www.f2er.com/3169017.html

大家都在问