![]() |
Download einer Datei
Ich habe bislang immer UrlDownloadToFile bzw. URLDownloadToCacheFile verwendet, um auf bestimmte Daten im Internet zuzugreifen. Unter anderem zum Download von Programmupdates.
Das hat auch problemlos funktioniert und funktioniert auch noch weiterhin auf Desktop-Rechnern. Aber auf aktuellen Windows Servern funktioniert das nicht mehr. Ich erhalte nach dem Aufruf eine Rückgabewert 2148270088 bzw. INET_E_DOWNLOAD_FAILURE Ich vermute, dass da der Internet Explorer und somit die Funktion nicht mehr funktioniert. Ich habe danach nach Alternativen gesucht, aber bin nicht richtig fündig geworden. Für Textdateien haben ich InternetReadFile gefunden, was sehr gut funktioniert, aber wenn ich binär-Dateien (Setup) herunterladen will, dann passt das nicht. Wahrscheinlich irgendein falsches Setting.
Code:
function GetInetFileAsString(const fileURL: string): string;
const C_BufferSize = 4096; var sAppName: string; hSession, hURL: HInternet; Buffer: array[0..C_BufferSize] of Byte; BufferLen: DWORD; strHeader: String; strPageContent: TStringStream; AUrl:String; begin Result := ''; AUrl := fileURL; sAppName := ExtractFileName(Application.ExeName); hSession := InternetOpen(PChar(sAppName), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0); try strHeader := 'Accept-Charset: utf-8'#13#10; hURL := InternetOpenURL(hSession, PChar(AURL), PChar(strHeader), Length(strHeader), 0, 0); try strPageContent := TStringStream.Create('', TEncoding.Default); try BufferLen := 1; while BufferLen >0 do begin If InternetReadFile(hURL, @Buffer[0], SizeOf(Buffer), BufferLen) then if BufferLen > 0 then strPageContent.WriteBuffer(Buffer[0], BufferLen); end; Result := strPageContent.DataString; finally strPageContent.Free; end; finally InternetCloseHandle(hURL); end finally InternetCloseHandle(hSession); end; end; Eine andere Variante über THTTPClient.Get funktioniert auf dem Desktop für binäre Dateien sehr gut, aber auf dem Server wird der Browser geöffnet und die Datei wird im Download-Ordner und nicht an der Stelle "DestFile" gespeichert, was keine Lösung ist.
Code:
Was wäre der beste und einfachste Weg, um auf einem aktuellen Windows-Server eine binäre Datei über eine URL herunterzuladen?
FS := TFileStream.Create( DestFile, fmCreate);
try HttpClient := THTTPClient.Create; try HttpClient.ConnectionTimeout := 5000; HttpClient.ResponseTimeout := 5000; lResponse := HttpClient.Get( SourceFile, FS); finally HttpClient.Free; end; finally FS.Free; end; FTP würde ich am liebsten für den Download vermeiden. |
AW: Download einer Datei
String-Result + TStringStream und Binär?
Natürlich, dass da was nicht passt. :zwinker: |
AW: Download einer Datei
Zitat:
You are using the OS InternetReadFile to download this will invoke OS network layer, these have a default security settings and policy, it is not only Firewall but what content you can downloaded, in most Windows Servers the Defender is disabled by default as it will hinder the OS, but the OS expects the User/Administrator for such platform to be experienced enough to set it right, so instead of depending on Defender they use many other settings to establish the defense line to prevent most critical settings that many just miss or not know about. I don't know what version you are talking about, but i am sure it is some policy somewhere. On other hand you can ditch INet and switch to any other library to perform download, Indy/ICS/RTC... but still you can search for the such policy and change it, i have no idea where is it, sorry ! |
AW: Download einer Datei
Zitat:
Aber was nutzt Ihr denn? |
AW: Download einer Datei
Ich habe jetzt eine Variante gefunden, die auf den ersten Blick zu funktionieren scheint:
![]()
Code:
function DownloadFile(
const url: string; const destinationFileName: string): boolean; var hInet: HINTERNET; hFile: HINTERNET; localFile: File; buffer: array[1..1024] of byte; bytesRead: DWORD; begin result := False; hInet := InternetOpen(PChar(application.title), INTERNET_OPEN_TYPE_PRECONFIG,nil,nil,0); hFile := InternetOpenURL(hInet,PChar(url),nil,0,0,0); if Assigned(hFile) then begin AssignFile(localFile,destinationFileName); Rewrite(localFile,1); repeat InternetReadFile(hFile,@buffer,SizeOf(buffer),bytesRead); BlockWrite(localFile,buffer,bytesRead); until bytesRead = 0; CloseFile(localFile); result := true; InternetCloseHandle(hFile); end; InternetCloseHandle(hInet); end; |
AW: Download einer Datei
Probier mal den stinknormalen Indy-TIDHTTP. Das ist deutlich einfacher und funktioniert problemlos.
|
AW: Download einer Datei
Alternativ kannst du auch mal in die Implementierung von
![]() ![]() |
AW: Download einer Datei
Alternativen wurden schon Einige genannt (von noch viel mehr),
ansonsten, bezüglich dieses Codes, z.B. TMemoryStream oder TFileStream, anstatt des TStringStream. |
Alle Zeitangaben in WEZ +1. Es ist jetzt 04:16 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz