方式一、原来用
ShellExecute
直接打开浏览器的方式,可以直接下载缓存中的文件
可是不能用于UI交互,而且用户能看到url地址
CString strUrl = “url。。。” CString strPrama; strPrama.Format(_T("?programId=%d&orgCode=%s&fileName=%s"), m_id , get_torg_code(), m_name); ShellExecute(NULL, _T("open"), strUrl + strPrama, NULL, NULL, SW_SHOWNORMAL);
方式二,用URLDownloadToFile,下载小文件还行,文件一旦过大(具体数字没有查看,没有查找瓶颈)可以下载100多KB的,下载299KB就失败
DeleteUrlCacheEntry(strPathUrl);
HRESULT hr = URLDownloadToFile(NULL, strUrl, stLocalFile, 0, NULL);
方式三,用curl,可以下载缓存,可以下载具体路径的
类中声明 BOOL CurlDownloadFile(CString strLocalFile); static size_t DealReqReply(void *buffer, size_t size, size_t nmemb, void *userp); //下载文件数据接收函数 回调函数需要是static或者全局函数
BOOL CDlgXXXX::CurlDownloadFile(CString strLocalFile) { BOOL bRet = FALSE; string szLocalFile = CStringFun::wstring_to_string(strLocalFile.GetBuffer(0)); strLocalFile.ReleaseBuffer(); FILE *fp = fopen(szLocalFile.c_str(), "wb"); if (fp == NULL) { return bRet; } CURL *curl = curl_easy_init(); struct curl_httppost *formpost = NULL; struct curl_httppost *lastptr = NULL; char cId[16] = {0}; _itoa_s(m_project->m_id, cId, 16, 10); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "programId", CURLFORM_COPYCONTENTS, cId, CURLFORM_END); CString strOrg = m_zj_libmgr->get_torg_code(); string szOrg = CStringFun::wstring_to_string(strOrg.GetBuffer()); strOrg.ReleaseBuffer(); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "orgCode", CURLFORM_COPYCONTENTS, szOrg.c_str(), CURLFORM_END); string szName = CStringFun::wstring_to_string(gZJHome->m_project->m_name.GetBuffer()); m_project->m_name.ReleaseBuffer(); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "fileName", CURLFORM_COPYCONTENTS, szName.c_str(), CURLFORM_END); CString strUrl =get_post_url(purl_get_version_url); string szUrl = CStringFun::wstring_to_string(strUrl.GetBuffer(0)); strUrl.ReleaseBuffer(); curl_easy_setopt(curl, CURLOPT_URL, szUrl.c_str()); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, FALSE); curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &DealReqReply); curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); CURLcode res = curl_easy_perform(curl); curl_formfree(formpost); curl_easy_cleanup(curl); fclose(fp); if(res == CURLE_OK) { bRet = TRUE; } return bRet; } size_t CDlgXXXX::DealReqReply(void *buffer, size_t size, size_t nmemb, void *userp) { FILE *fp = (FILE *)userp; size_t return_size = fwrite(buffer, size, nmemb, fp); return return_size; }
使用
void CDlgXXXX::OnBnClickedDown() { CString strName = m_project->m_name; strName.Replace(_T(":"), _T("-")); strName.Replace(_T("/"), _T("-")); CFileDialog dlg(FALSE, _T("*.xls"), strName, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("Excel Files (*.xls)|*.xls||")); if(dlg.DoModal() == IDOK) { CWaitCursor cur; m_btn_down_.EnableWindow(FALSE); CString strLocalFile = dlg.GetPathName(); BOOL bRet = CurlDownloadFile(strLocalFile); if (bRet) { ShellExecute(NULL, _T("open"), dlg.GetFolderPath(), NULL, NULL, SW_SHOWNORMAL); } else { CString strUrl = get_post_url(purl_get_version_url); CString strPrama; strPrama.Format(_T("?programId=%d&orgCode=%s&fileName=%s"), m_id , get_torg_code(), m_name); ShellExecute(NULL, _T("open"), strUrl + strPrama, NULL, NULL, SW_SHOWNORMAL); } m_btn_down_.EnableWindow(TRUE); } }