从触发器下载delphi中的文件并捕获文件名 [英] Downloading a file in delphi from a trigger and capturing the file name

查看:291
本文介绍了从触发器下载delphi中的文件并捕获文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站的网址。看起来像这样: http://www.example.com/downloads/file/4789 / download



我想将文件保存到我的系统,但我不知道如何获取下载所触发的下载文件名我的例子中的URL有些文件是pdf其他文件是doc和rtf等。



如果有人可以指出我的文件名问题的方向,还有什么组件要使用,我真的很感激

解决方案

从url获取文件名,您可以检索HEAD信息,并检查 Content Disposition 标题字段。对于此任务,您可以使用TIdHTTP indy组件。如果内容处分没有文件名,您可以尝试解析URL。



尝试此示例。

  {$ APPTYPE CONSOLE} 

{$ R * .res}

使用
IdURI,
IdHttp,
SysUtils;

函数GetRemoteFileName(const URI:string):string;
var
LHttp:TIdHTTP;
begin
LHttp:= TIdHTTP.Create(nil);
try
LHttp.Head(URI);
结果:= LHTTP.Response.RawHeaders.Params ['Content-Disposition','filename'];
if Result =''then
with TIdURI.Create(URI)do
try
Result:= Document;
终于
免费;
结束
finally
LHttp.Free;
结束
结束

begin
try
Writeln(GetRemoteFileName('http://dl.dropbox.com/u/12733424/Blog/Delphi%20Wmi%20Code%20Creator/Setup_WmiDelphiCodeCreator.exe '));
Writeln(GetRemoteFileName('http://studiostyl.es/settings/downloadScheme/1305'));

除了
对于E:异常do
Writeln(E.ClassName,':',E.Message);
结束
Readln;
结束。


I have a url of a website. It looks something like this: http://www.example.com/downloads/file/4789/download?

I would like to save the file to my system, but I do not know how to get the file name of the download triggered by the URL in my example. Some files are pdf others are doc and rtf etc.

If someone can please point me in a direction of the filename problem and also what components to use, I would really appreciate it.

解决方案

to get the filename from a url you can retrieve the HEAD information and check Content Disposition header field. For this task you can use the TIdHTTP indy component. if the Content Disposition doesn't have the file name you can try parsing the url.

Try this sample .

{$APPTYPE CONSOLE}

{$R *.res}

uses
  IdURI,
  IdHttp,
  SysUtils;

function GetRemoteFileName (const URI: string) : string;
var
  LHttp: TIdHTTP;
begin
  LHttp := TIdHTTP.Create(nil);
  try
    LHttp.Head(URI);
    Result:= LHTTP.Response.RawHeaders.Params['Content-Disposition', 'filename'];
    if Result = '' then
     with TIdURI.Create(URI) do
      try
       Result := Document;
      finally
       Free;
      end;
  finally
    LHttp.Free;
  end;
end;

begin
  try
    Writeln(GetRemoteFileName('http://dl.dropbox.com/u/12733424/Blog/Delphi%20Wmi%20Code%20Creator/Setup_WmiDelphiCodeCreator.exe'));
    Writeln(GetRemoteFileName('http://studiostyl.es/settings/downloadScheme/1305'));

  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;
end.

这篇关于从触发器下载delphi中的文件并捕获文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆