掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
WinInetを使ったHTTPでのファイルダウンロードについて (ID:12108)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
いろいろやっていたら解決できました。 TextFileではなく、Fileを使うといいようです。 WinInet_URLDownloadToFile1とWinInet_URLDownloadToFile2を 以下のように書きかえる正しく動作しています。 メモリ確保などが複雑な順番なので 間違っていましたらご指摘ください。 function WinInet_URLDownloadToFile1(URL: String; FileName: String): Boolean; var hSession, hService: HINTERNET; lpBuffer: PChar; dwBytesRead: DWORD; dwBytesAvailable: DWORD; fp: File; begin Result := False; hSession := InternetOpen( 'MyApp', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0 ); try if Assigned( hSession ) then begin hService := InternetOpenUrl( hSession, PChar(URL), nil, 0, 0, 0); try if Assigned( hService ) then begin AssignFile(fp, FileName); try Rewrite(fp, 1); while true do begin dwBytesRead := 0; InternetQueryDataAvailable( hService, dwBytesAvailable, 0, 0 ); GetMem(lpBuffer, dwBytesAvailable); try InternetReadFile( hService, lpBuffer, dwBytesAvailable, dwBytesRead ); BlockWrite(fp, lpBuffer^, dwBytesRead ); if dwBytesRead = 0 then break; finally FreeMem(lpBuffer); end; end; finally CloseFile(fp); end; Result := True; end; finally InternetCloseHandle( hService ); end; end; finally InternetCloseHandle( hSession ); end; end; function WinInet_URLDownloadToFile2(URL: String; FileName: String): Boolean; var hSession, hService: HINTERNET; lpBuffer: PChar; dwBytesRead: DWORD; fp: File; begin Result := False; hSession := InternetOpen( 'MyApp', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0 ); try if Assigned( hSession ) then begin hService := InternetOpenUrl( hSession, PChar(URL), nil, 0, 0, 0); try if Assigned( hService ) then begin AssignFile(fp, FileName); try Rewrite(fp, 1); GetMem(lpBuffer, 1024); try while true do begin dwBytesRead := 0; InternetReadFile( hService, lpBuffer, 1024, dwBytesRead); BlockWrite(fp, lpBuffer^, dwBytesRead ); if dwBytesRead = 0 then break; end; finally FreeMem(lpBuffer); end; finally CloseFile(fp); end; Result := True; end; finally InternetCloseHandle( hService ); end; end; finally InternetCloseHandle( hSession ); end; end;
←解決時は質問者本人がここをチェックしてください。
戻る
掲示板システム
Copyright 2021 Takeshi Okamoto All Rights Reserved.