Текущий архив: 2008.04.27;
Скачать: CL | DM;
Вниз
Проблема со скачиванием файла, WinInet Найти похожие ветки
← →
Dima (2008-04-01 12:55) [0]Здравствуйте,
я пытаюсь скачать файл (exe, dll, zip, rar... бывает и txt) с http сервера используя WinInet.
Мой код:function HTTP (PathToFile: string):string;
var
hSession: HINTERNET;
hService: HINTERNET;
lpBuffer: array[0..1023 + 1] of Char;
dwBytesRead: DWORD;
FileName: string;
SpecCharNum: Integer;
F: TextFile;
begin
Result:= "";
hSession:= InternetOpen("svchost", INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
try
if Assigned(hSession) then
begin
FileName:= PathToFile;
SpecCharNum:=1;
while (SpecCharNum <> 0) do
begin
SpecCharNum:=pos ("/", FileName);
if (SpecCharNum=0) then
break;
FileName:= copy(FileName,SpecCharNum+1,Length(FileName)-SpecCharNum+1);
end;
AssignFile(F, FileName);
Rewrite(F);
CloseFile(F);
hService:= InternetOpenUrl(hSession, PChar(PathToFile), nil, 0, 0, 0);
if Assigned(hService) then
try
while True do
begin
dwBytesRead:= 1024;
InternetReadFile(hService, @lpBuffer, 1024, dwBytesRead);
if (dwBytesRead = 0) then break;
lpBuffer[dwBytesRead]:= #0;
AssignFile(F, FileName);
Append(F);
Write(F, lpBuffer);
CloseFile(F);
end;
finally
InternetCloseHandle(hService);
end;
end;
finally
InternetCloseHandle(hSession);
end;
end;
если я вызываю функцию так:HTTP ("http://ip/files/file.txt");
то все нормально
если я вызываю функцию так:HTTP ("http://ip/files/file.zip");
то файл скачивается поврежденным
Господа, в чем проблема??
Спасибо
← →
Сергей М. © (2008-04-01 13:09) [1]
> файл скачивается поврежденным
Ничего подобного, скачивается он нормальным.
Гадишь полученный контент ты уже сам, своими ручками:
> F: TextFile;
← →
Dima (2008-04-02 16:16) [2]Спасибо.
А в таком случае надоF: File;
илиF: File of char;
?
Честно сказать, я плохо понимаю данный момент
← →
Сергей М. © (2008-04-02 16:25) [3]Ни то ни другое.
Проще всего использовать объект класса TFileStream:
MyFileStream.WriteBuffer(lpBuffer, dwBytesRead);
← →
Dima (2008-04-02 16:31) [4]аа.. а в случае консольных приложений?
← →
Reindeer Moss Eater © (2008-04-02 16:35) [5]Хоть законсольных
← →
Stepper (2008-04-02 23:15) [6]
> Dima (01.04.08 12:55)
Вот то что тебе надо: http://www.delphisources.ru/pages/faq/base/archives/downloader_code.zip
;)
Страницы: 1 вся ветка
Текущий архив: 2008.04.27;
Скачать: CL | DM;
Память: 0.48 MB
Время: 0.021 c