如何通过url得到下载的文件名?

lly520tyt 2007-11-21 08:32:16
如何通过url得到下载的文件名?
url例如http://www.xxxx.com/attachment.php?id=113之类,象迅雷和flashget是怎么得到下载这个url的文件名及文件size的?最好能给个delphi的代码
...全文
970 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuehaiyang 2007-11-28
  • 打赏
  • 举报
回复
没有做过,关注
lly520tyt 2007-11-28
  • 打赏
  • 举报
回复
这里是获取下载文件名的代码,但在实际应用里效果不如迅雷的,总有些得不到文件名,或者得到的文件名不正确,请提供一个获取文件名的正确方法
procedure TForm1.Button1Click(Sender: TObject);
var lpBuffer: Array [0..4096] of Char;
hOpen: HINTERNET;
hOpenURL: HINTERNET;
hFile: Integer;
lpszFile: PChar;
dwIndex: DWORD;
dwSize: DWORD;
begin

// Open the connection handle
hOpen:=InternetOpen('Delphi Agent', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil,
0);

// Check handle
if Assigned(hOpen) then
begin
// Resource protection
try
// Open the URL
hOpenURL:=InternetOpenUrl(hOpen,
'http://www.censusdata.abs.gov.au/ABSNavigation/download?format=xls&collection=Census&period=2006&productlabel=Ancestry%20by%20Country%20of%20Birth%20of%20Parents&producttype=Census%20Tables&method=Place%20of%20Usual%20Residence&areacode=SSC16003',
nil, 0, INTERNET_FLAG_NEED_FILE, 0);
// Check handle
if Assigned(hOpenURL) then
begin
// Resource protection
try
// Set index
dwIndex:=0;
// Set size of buffer
dwSize:=SizeOf(lpBuffer);
// Query for the content disposition
if HttpQueryInfo(hOpenURL, HTTP_QUERY_CONTENT_DISPOSITION,
@lpBuffer, dwSize, dwIndex) then
begin
// Scan for the filename= in the string
dwIndex:=Pos('filename=', lpBuffer);
// Check for match
if (dwIndex > 0) then
begin
// Set filename string
lpszFile:=@lpBuffer[dwIndex + 8];
// Create file with desired name
hFile:=FileCreate(lpszFile);
// Check handle
if not(hFile = (-1)) then
begin
// Resource protection
try
// Read from connection and write contents
while (InternetReadFile(hOpenURL, @lpBuffer,
SizeOf(lpBuffer), dwSize) and (dwSize > 0)) do
begin
// Write to the file
FileWrite(hFile, lpBuffer, dwSize);
end;
finally
// Close the file
FileClose(hFile);
end;
end;
end;
end;
finally
// Close handle
InternetCloseHandle(hOpenURL);
end;
end;
finally
// Close handle
InternetCloseHandle(hOpen);
end;
end;

end;
lly520tyt 2007-11-25
  • 打赏
  • 举报
回复
在一个老外的网站上看到答案了
uses wininet
internetopen
internetopenurl
httpqueryinfo 参数http_query_disposion
在得到的参数里找filename=字符串
不过这种方法只对没有直接提供文件名的有效,已经直接提供文件名的不起作用
lly520tyt 2007-11-24
  • 打赏
  • 举报
回复
找到tiduri类了,这个只是单纯的从给的url进行字符串的分析,它得到的文件名是attachment.php,而我要的是得到下载的原来的文件名。
都说用httpqueryinfo http_query_content_disposion http_query_content_description但从msdn上看这两个参数已经过时不用了,得不到下载的文件名。
到底迅雷、flashget是怎么得到下载文件名的
lly520tyt 2007-11-24
  • 打赏
  • 举报
回复
TIdURI是什么类?Delphi提示没有定义
我用的是Delphi7 SE
lly520tyt 2007-11-24
  • 打赏
  • 举报
回复
已经设置了handleredirects,有些网站是可以得到真实地址的,但我前面提到的那种,根本没有产生转向事件。
head取不回这些地址的信息
tiduri怎么用?能说一下吗
小风哥哥 2007-11-24
  • 打赏
  • 举报
回复
重定向后的地址最好在OnRedirect时间里获取,有时重定向了但是idhttp.Response.Location还是空的
小风哥哥 2007-11-24
  • 打赏
  • 举报
回复
idhttp.Response.Location // 真实地址
真实地址里有你要的文件名, 名字都是通过地址分析得来的
小风哥哥 2007-11-23
  • 打赏
  • 举报
回复
文件名分析可以通过TIdURI 类来做,很简单
小风哥哥 2007-11-23
  • 打赏
  • 举报
回复
idhttp.HandleRedirects := true;
try
idhttp.head(url);
except

end;

idhttp.Response.ContentLength // 大小
idhttp.Response.Location // 真实地址

1,593

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 网络通信/分布式开发
社区管理员
  • 网络通信/分布式开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧