`
eueuy
  • 浏览: 74496 次
  • 性别: Icon_minigender_1
  • 来自: 天津
最近访客 更多访客>>
社区版块
存档分类
最新评论

下载文件 Get File From the Net

阅读更多

Get File From the Net
Create a Delphi application that downloads files from the Internet; add an auto-update feature to your programs. <!-- BEGIN FREQUENCY TEMPLATE -->
<!--more of this feature. Use to link to multi-page features on your own site.--><!--end more of this feature--> <!--join the discussion. Use to link to relevant forum thread on your site.--><!--end join the discussion--><!--related resources. Use to link to related content on YOUR site.--><!--end related resources--><!--from other guides. Use to link to related "Web Ring" or coordinated content on OTHER Guide sites.--><!--end from other guides--><!--elsewhere on the web. Use to link to external sites on the Web.--><!--end elsewhere on the web--><!--rate this article.--><!--end rate this article-->
 Join the Discussion
"Post your views, or look for guidance on the questions and issues that matter to creating a simple ftp download manager in Delphi"
Discuss!
  Related Resources
· Copy files with Delphi
· Internet Programming
· My IP with Delphi
· Are we OnLine?
 Elsewhere on the Web
· Console type FTP
· WinInet API reference
<!-- Begin Poll Code -->
About Poll
This [013001] article is:
Awesome
Great
Ok
Not bad
Poor
Current Results
<!-- End Poll Code -->
 
<!--END FREQUENCY TEMPLATE-->

<!-- BEGIN ARTICLE CONTENT -->

If your program relies on Packages or DLLs than deploying new versions of your libraries certainly takes some time. The Internet now provides a fairly easy way to accomplish this task. Adding an auto-update option to your applications could be the best way to keep your Delphi applications up to date.

Let's see how to create the most important piece of code in any FTP application.

Delphi gives us full access to the WinInet API (wininet.pas) which we can use to connect to and retrieve files from any Web site that uses either Hypertext Transfer Protocol (HTTP) or File Transfer Protocol (FTP). For example, we could use the functions inside the WinInet API to: add an FTP browser to any application, create an application that automatically downloads files from a public FTP site or search the Internet site for references to graphics and download only the graphics.

GetInetFile function

uses Wininet;

function GetInetFile
(const fileURL, FileName: String): boolean;
const BufferSize = 1024;
var
  hSession, hURL: HInternet;
  Buffer: array[1..BufferSize] of Byte;
  BufferLen: DWORD;
  f: File;
  sAppName: string;
begin
 Result:=False;
 sAppName := ExtractFileName(Application.ExeName);
 hSession := InternetOpen(PChar(sAppName),
                INTERNET_OPEN_TYPE_PRECONFIG,
               nil, nil, 0);
 try
  hURL := InternetOpenURL(hSession,
            PChar(fileURL),
            nil,0,0,0);
  try
   AssignFile(f, FileName);
   Rewrite(f,1);
   repeat
    InternetReadFile(hURL, @Buffer,
                     SizeOf(Buffer), BufferLen);
    BlockWrite(f, Buffer, BufferLen)
   until BufferLen = 0;
   CloseFile(f);
   Result:=True;
  finally
   InternetCloseHandle(hURL)
  end
 finally
  InternetCloseHandle(hSession)
 end
end;

Note: In order to provide some visual feedback to the user you could add a line like FlashWindow(Application.Handle,True) in the body of the repeat/until block. The FlashWindow API call flashes the caption of your applications name in the task bar.

Usage
To call the GetInetFile function you could use the next peace of code:

var FileOnNet, LocalFileName: string
begin
 FileOnNet:=
  'http://delphi.about.com/library/forminbpl.zip';
 LocalFileName:='File Downloaded From the Net.zip'

 if GetInetFile(FileOnNet,LocalFileName)=True then
  ShowMessage('Download successful')
 else
  ShowMessage('Error in file download')

end;

This code will get the 'forminbpl.zip' file from this site, download it, and save it as 'File Downloaded From the Net.zip'.

Note: Depending on which version of Delphi you have, several components can be found on the Internet page of the VCL palette that are designed to ease the task of Internet enabling your applications. For an example of a FTP component look for TNMFTP in the FastNet VCL page. <!-- END ARTICLE CONTENT -->

<!-- START ARTICLE NEW BOTTOM -->

<!-- START ARTICLE NEW BOTTOM -->
分享到:
评论

相关推荐

    File_实用案例_实现文件拷贝_FileCopy.java

    // Get File objects from Strings File to_file = new File(to_name); // First make sure the source file exists, is a file, and is readable. if (!from_file.exists()) abort("no such source file: " ...

    Get the hard disk serial number or Motherboard serial number in java

    2. 使用Java的`File`类创建并写入这个VBScript文件。 3. 运行VBScript文件,通过`Runtime.getRuntime().exec()`方法调用`cscript`命令执行VBScript,并捕获其输出。 4. 读取并处理VBScript的输出,这将是主板序列号...

    Softimage ICE节点从波形文件中获取频谱 Softimage ICE Node to get the Spectrum

    "Softimage ICE Node to get the Spectrum from a Wave file" 这个标题意味着我们有一个ICE节点,它的功能是读取.WAV格式的音频文件,并从中计算出频谱信息。WAV是一种无损音频文件格式,广泛用于存储各种音频数据,...

    Windows读写Ext2/Ext3/Ext4文件系统4

    How to get safe and quick access to any file systems? DiskInternals Linux Reader is an easy and short way to solve the problem! While saving, it ignores file security policies. It means that it is ...

    org.apache.commons.httpclient 远程下载文件

    System.out.println("Successfully downloaded file from " + remoteUrl + " to " + localFilePath); } catch (IOException e) { System.err.println("An error occurred while downloading the file: " + e....

    C#压缩文件

    本文档将详细讲解如何使用C#来压缩多个文件到一个ZIP文件中,特别强调的是只压缩指定文件夹中的文件而不包括子文件夹中的文件。 #### 二、核心代码解析 首先,我们来看一下关键的代码片段: ```csharp using ...

    delphi文件搜索控件

    At 'Unit file name': Browse and Select EasyFileSearchReg.pas from the directory where you installed it. At 'Package file name': TEasyFileSearch ( In the directory Projects\Lib ) At 'Description': ...

    Oracle P/L SQL实现FTP上传、下载功能

    Oracle P/L SQL实现FTP上传、下载功能,以下是此过程包的头部,包体经常打包处理plb,感兴趣用户可以下载下来。 --Oracle上的FTP功能 Create or Replace Package UTL_FTP AUTHID CURRENT_USER as Type Connection ...

    pyfsig:python 文件签名标识符 - 使用它从文件头中识别文件

    derp = get_from_file &#40; f &#41; # returns Match object (list of signatures) that match the file at this file path herp = get_from_path ( f_name ) # this can be a string or a bytestring, will raise...

    java 缓存数据索引文件

    Query the data from table (database)with the tremendous volume or from the file system, the efficiency may be very slow. While using QuickMemoryFile, regard data table or file system as the data ...

    HTMLViewer 支持Delphi 6 - XE6

    * Additional code and/or components are generally required to get data from other sources than the local file system. These components support most of the HTML 3.2 specifications with many additional...

    通过api上传文件

    在这个例子中,我们使用了cURL库代替`file_get_contents()`,并编码了文件内容为base64,以适应API的要求。 在实际开发中,你还需要考虑错误处理、文件大小限制、文件类型验证、API认证(如OAuth)等问题。确保在上...

    模拟GET/POST方法发送HTTP请求【chenjinxing_webhttp2.rar】

    add a file upload function, there is another find from the delphibbs file upload component, I also be placed in compression bag, and the other is mainly structural adjustment programs, hoping to ...

    java内存文件快速查找

    Query the data from table (database)with the tremendous volume or from the file system, the efficiency may be very slow. While using QuickMemoryFile, regard data table or file system as the data ...

    2009 达内Unix学习笔记

    用法 touch [-acm] [-r ref_file] 文件... touch [-acm] [MMDDhhmm[yy]] 文件... touch [-acm] [-t [[CC]YY]MMDDhhmm[.SS]] file... mkdir 创建目录(必须有创建目录的权限) 用法 mkdir [-m 模式] [-p] dirname ....

    Thinkphp rce扫描脚本,附带日志扫描

    1. 增加使用代理池功能 2. 增加输出到文件功能 3. 去掉了一些使用syetem函数的... -f FILE, --file FILE read the url from the file -p PROXY, --proxy PROXY use HTTP/HTTPS proxy --shell try to get shell

    android之网络文件上传下载源码.pdf

    `FromFile.java`类可能是用于封装文件信息的类,如文件名、文件内容、参数名和内容类型。例如: ```java package com.cn; import java.io.File; import java.io.FileInputStream; import java.io....

    .htaccess

    you can enter a command into your .htaccess file to stop the directory list from being shown: 为避免这种情况(而不必创建一堆的新index文件),你可以在你的.htaccess文档中键入以下命令,用以阻止目录...

    ES组件elasticsearch-head报错 npm ERR! Please include the following file with any support request

    Please include the following file with any support request"是npm(Node.js包管理器)在遇到问题时给出的通用错误提示,要求在寻求支持时附带上npm-debug.log日志文件,这个文件通常包含了详细的错误信息,帮助...

    阿里云镜像安装docker报错的问题及解决方案

    Malformed entry 60 in list file /etc/apt/sources.list (Component) 解决方案 注意到报错提示/etc/apt/sources.list 这个文件第60行格式错误,那么尝试把这行注释掉。先打开这个文件: $ sudoedit /etc/apt/...

Global site tag (gtag.js) - Google Analytics