C + + Download pictures in bulk based on image URL

Source: Internet
Author: User
Tags lenovo

Recently, you need to use the bulk download to local based on the image URL. Found the relevant information, recorded here.

1. First extract the URL from the CSV file

Ifstream Fin ("C:\\users\\lenovo\\desktop\\query_result0503.csv");//Open File stream operation    stringLine ; intCNT =0;  while(Getline (Fin, line) && CNT < -) {Istringstream sin (line);//reads an entire line of string lines into the string stream Istringstreamvector<string>URLs; stringURL;  while(Getline (sin, url,','))//comma-delimited{urls.push_back (URL); } CNT++; size_t found= urls[0].find_last_of ("/\\"); if(Found = =string:: NPOs)Continue; stringImgname = urls[0].substr (found+1); cout<<"after processing:"<< urls[0] <<"----"<<cnt<<"----"<
   }

2. Save the picture locally based on the URL.

You need to use the Urldownloadtofile function.

The main step is to use the wide character parameter for this function. If the parameter type is incorrect: Then you don't want to download the correct file ...

You need to use the MultiByteToWideChar function to convert ....

HRESULT urldownloadtofile (             lpunknown            pcaller,             lpctstr              szurl,             lpctstr              szFileName,  _ Reserved_ DWORD                dwreserved,             lpbindstatuscallback LPFNCB);
Parameters
  • Pcaller
    A pointer to the controlling IUnknown interface of the calling ActiveX component, if the caller was an ActiveX component. If The calling application is not a ActiveX component, this value can set to NULL. Otherwise, the caller is a COM object which is contained in another component, such as an ActiveX control in the context of An HTML page. This parameter represents the outermost IUnknown of the calling component. The function attempts the download in the context of the ActiveX client framework, and allows the caller container to Rece Ive callbacks on the progress of the download.

  • Szurl
    A pointer to a string value, contains the URL to download. Cannot is set to NULL. If the URL is invalid, Inet_e_download_failure is returned.

  • szFileName
    A pointer to a string value containing the name, or full path of the file to create for the download. If szfilenameincludes a path, the target directory must already exist.

  • Dwreserved
    Reserved. Must is set to 0.

  • Lpfncb
    A pointer to the IBindStatusCallback interface of the caller. By using ibindstatuscallback::onprogress, a caller can receive download status. Urldownloadtofile calls the Ibindstatuscallback::onprogress and Ibindstatuscallback::ondataavailable methods as data is Received. The download operation can be canceled by returning E_abort from any callback. This parameter can was set to NULL if the status is not required.

Return value

This function can return one of these values.

Return Code Description
S_OK

The download started successfully.

E_outofmemory

The buffer length is invalid, or there are insufficient memory to complete the operation.

Inet_e_download_failure

The specified resource or callback interface was invalid.

///size_t len = urls[0].length ();//Get string length        intNmlen = MultiByteToWideChar (CP_ACP,0, urls[0].c_str (), Len +1Null0);//If the function runs successfully and Cchwidechar is zero,//The return value is the size of the wide number of characters required to receive the buffer for the string to be converted. wchar_t* buffer =NewWchar_t[nmlen]; MultiByteToWideChar (CP_ACP,0, urls[0].c_str (), Len +1, buffer, nmlen); stringSavepath ="c:\\users\\lenovo\\desktop\\csvfile\\query_result0423\\"+imgname; size_t len1=savepath.length (); wchar_t* Imgsavepath =NewWCHAR_T[LEN1]; intNmlen1 = MultiByteToWideChar (CP_ACP,0, Savepath.c_str (), Len1 +1Null0); MultiByteToWideChar (CP_ACP,0, Savepath.c_str (), Len1 +1, Imgsavepath, NMLEN1); HRESULT HR= Urldownloadtofile (NULL, buffer, Imgsavepath,0, NULL); if(hr = =S_OK) {cout<<"-------OK"<<Endl; }

Reference:

Http://www.cnblogs.com/codingmengmeng/p/6258020.html

https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/ Ms775123 (v=vs.85) #parameters

C + + Download pictures in bulk based on image URL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.