unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
StdCtrls; type
TForm1 = class(TForm)
btnReceive: TButton;
IdTCPClient1: TIdTCPClient;
btnSend: TButton;
procedure btnReceiveClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure btnSendClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.dfm} procedure TForm1.btnReceiveClick(Sender: TObject);
var
rbyte:array[..] of byte;
sFile:TFileStream;
iFileSize:integer;
begin
iFileSize:=IdTCPClient1.ReadInteger; sFile:=TFileStream.Create('xx.pdf',fmCreate);
While iFileSize> do
begin
IdTCPClient1.ReadBuffer(rbyte,);// .ReadBuffer(rbyte,iLen);
sFile.Write(rByte,);
inc(iFileSize,-);
end;
IdTCPClient1.ReadBuffer(rbyte,iFileSize);// .ReadBuffer(rbyte,iLen);
sFile.Write(rByte,iFileSize);
sFile.Free;
ShowMessage('file get ok!');
end; procedure TForm1.FormCreate(Sender: TObject);
begin
// IdTCPClient1.BoundIP:= '127.0.0.1';
// IdTCPClient1.BoundPort:= 90011; IdTCPClient1.Host:= '127.0.0.1';
IdTCPClient1.Port:= ; IdTcpClient1.Connect();
self.Caption:= '启动';
end; procedure TForm1.btnSendClick(Sender: TObject);
var
iFileHandle:integer;
iFileLen,cnt:integer;
buf:array[..] of byte; begin
// IdTCPClient1.Host:='127.0.0.1';
// IdTCPClient1.Port:=9000; //IdTCPClient1.Connect(5000); if IdTCPClient1.Connected then
begin
iFileHandle:=FileOpen('xx.pdf',fmOpenRead);
iFileLen:=FileSeek(iFileHandle,,); //2 :soFromBeginning : from the end of the file
FileSeek(iFileHandle,,);
// ProgressBar1.Max:=iFileLen;
// ProgressBar1.Position := 0;
IdTCPClient1.WriteLn('xx.pdf'+'|'+IntToStr(iFileLen));
while true do
begin
Application.ProcessMessages;
cnt:=FileRead(iFileHandle,buf,);
IdTCPClient1.WriteBuffer(buf,cnt);
// IdTCPClient1.WriteInteger(cnt); // ProgressBar1.Position:=ProgressBar1.Position + cnt;
// StatusBar1.Panels[0].Text:='正在传送文件...';
if cnt< then
break;
end; FileClose(iFileHandle);
// Label2.Caption:='文件传送完成!';
// StatusBar1.Panels[0].Text:='文件传送完成!';
end;
end; end. unit Unit1; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPServer; type
TForm1 = class(TForm)
IdTCPServer1: TIdTCPServer;
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
procedure FormCreate(Sender: TObject);
procedure IdTCPServer1Connect(AThread: TIdPeerThread);
private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.dfm}
uses IdSocketHandle; //server 接收
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
rbyte:array[..] of byte;
sFile:TFileStream;
cnt,cmd,FileSize:integer;
str,FileName:string;
begin
str:=AThread.Connection.ReadLn; //接收文件大小及文件名 cmd:=pos('|',str); //查找分隔符
FileName:=copy(str,,cmd-); //提取文件名
//IdTCPClient1.WriteLn(ExtractFileName(edtFileName.Text)+'|'+IntToStr(iFileLen));
FileSize:=StrToInt(copy(str,cmd+,Length(str)-cmd+)); //提取文件大小
if MessageBox(,Pchar('用户 '+AThread.Connection.Socket.Binding.PeerIP+'要给您传送文件 "'+FileName+'" 您是接受还是拒绝?'),'文件接受',MB_YesNo or MB_ICONQUESTION)=ID_Yes then //询问是否接收
begin
// ProgressBar1.Max:=FileSize; //初始化进度条
// ProgressBar1.Position:=0;
// SaveDialog1.FileName:=FileName; //指定保存的默认文件名,一定要在 SaveDialog1.Execute;之前,不然文件名为空
// SaveDialog1.Execute; //sFile:=TFileStream.Create(SaveDialog1.FileName,fmCreate); //创建待写入的文件流
sFile:=TFileStream.Create(FileName,fmCreate); //创建待写入的文件流
While FileSize> do
begin
AThread.Connection.ReadBuffer(rbyte,);// 读取文件流
sFile.Write(rByte,); //写入文件流
// cnt:=AThread.Connection.ReadInteger; //从发送端接收最新的进度位置信息
// ProgressBar1.Position:=ProgressBar1.Position+cnt; //更新显示进度
// Label1.Caption:='当前接收进度..';
// StatusBar1.Panels[0].Text:='正在接收文件中...';
inc(FileSize,-);
end;
AThread.Connection.ReadBuffer(rbyte,FileSize);// .ReadBuffer(rbyte,iLen);
sFile.Write(rByte,FileSize);
sFile.Free;
// StatusBar1.Panels[0].Text:='文件接收完成!';
// Label1.Caption:='文件接收完成!';
end;
END; procedure TForm1.FormCreate(Sender: TObject);
var
h:TIdSocketHandle;
begin
begin
IdTCPServer1.DefaultPort:=;
// h:= IdTCPServer1.Bindings.Add;
// h.IP:= '127.0.0.1';
// h.Port:=90011;
IdTCPServer1.Active:=True;
Self.Caption:= '启动';
end;
end; //服务端 发送文件
procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
var
iFileHandle:integer;
iFileLen,cnt:integer;
buf:array[..] of byte;
begin
iFileHandle:=FileOpen('xx.pdf',fmOpenRead);
iFileLen:=FileSeek(iFileHandle,,);
FileSeek(iFileHandle,,);
AThread.Connection.WriteInteger(iFileLen);
while true do
begin
cnt:=FileRead(iFileHandle,buf,);
AThread.Connection.WriteBuffer(buf,cnt);
if cnt< then
break;
end;
FileClose(iFileHandle);
end; end.

idtcp实现文件下载和上传的更多相关文章

  1. Pikachu漏洞练习平台实验——不安全的文件下载和上传(七)

    1.不安全的文件下载 1.1.概述 文件下载功能在很多web系统上都会出现,一般我们当点击下载链接,便会向后台发送一个下载请求,一般这个请求会包含一个需要下载的文件名称,后台在收到请求后 会开始执行下 ...

  2. pikachu-不安全的文件下载和上传

    一.文件下载漏洞 1.1 概述     很多网站都会提供文件下载的功能,即用户可以通过点击下载链接,下载到链接所对应的文件.但是,如果文件下载功能设计不当,则可能导致攻击者可以通过构造文件路径,从而获 ...

  3. day58:Linux:BashShell&linux文件管理&linux文件下载上传

    目录 1.BashShell 2.Linux文件管理 3.Linux文件下载和上传 BashShell 1.什么是BeshShell? 命令的解释,用来翻译用户输入的指令 2.BashShell能做什 ...

  4. [实战]MVC5+EF6+MySql企业网盘实战(12)——新建文件夹和上传文件

    写在前面 之前的上传文件的功能,只能上传到根目录,前两篇文章实现了新建文件夹的功能,则这里对上传文件的功能进行适配. 系列文章 [EF]vs15+ef6+mysql code first方式 [实战] ...

  5. web开发实战--图片裁剪和上传

    前言: 最近的开发中, 有一个上传头像的任务. 由于头像本身的特殊性, 其一般流程为选择图片, 编辑裁剪区域, 再继而上传图片操作. 看似简单的东西, 实则是挺麻烦的一件事. 借助这次开发机会, 来具 ...

  6. 基于uploadify.js实现多文件上传和上传进度条的显示

    uploadify是JQuery的一个插件,主要实现文件的异步上传功能,可以自定义文件大小限制.文件类型.是否自动上传等属性,可以显示上传的进度条.官网地址是http://www.uploadify. ...

  7. 重新想象 Windows 8 Store Apps (66) - 后台任务: 下载和上传

    [源码下载] 重新想象 Windows 8 Store Apps (66) - 后台任务: 下载和上传 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 后台任务 后台 ...

  8. 重新想象 Windows 8.1 Store Apps (91) - 后台任务的新特性: 下载和上传的新特性, 程序启动前预下载网络资源, 后台任务的其它新特性

    [源码下载] 重新想象 Windows 8.1 Store Apps (91) - 后台任务的新特性: 下载和上传的新特性, 程序启动前预下载网络资源, 后台任务的其它新特性 作者:webabcd 介 ...

  9. div+css网页本地上和上传到服务器后在IE11上看到的效果不一样?

    div+css网页本地上和上传到服务器后在IE11上看到的效果不一样? 解决办法在html的head里加上一段:<meta http-equiv="X-UA-Compatible&qu ...

随机推荐

  1. 十八、JavaScript之布尔类型

    一.代码如下 二.运行效果 <!DOCTYPE html> <html> <meta http-equiv="Content-Type" conten ...

  2. 移动MAS短信平台发送短信

    MAS短信平台发送短信分为两种方式 参考文档下载 一.sdk调用 using mas.ecloud.sdkclient; using System; namespace 短信发送 { class Pr ...

  3. HDU_4939 stupid tower defense 2014多校7 多变量型DP

    意思是有个塔防游戏,有三种塔,红塔在怪物经过的时候每秒会产生攻击力大小的伤害,绿塔对怪物经过以及经过之后每秒产生攻击力大小的伤害,还有种蓝塔,对怪物进行减速,即怪物从此之后经过一个单位都会减慢c秒 最 ...

  4. HDU 4952 Number Transformation 多校8 机智数学

    哎.这个题想了好久,状态不对啊...一个大家都出的题..当时想到肯定是可以有什么规律来暴力,不用算到10的10次方 对于某个k,x.从1到k循环,每次求一个新的x,这个x要大于等于原x,并且要是i的倍 ...

  5. 备份CSDN

    说明:https://blog.csdn.net/Feynman1999/article/details/87908082 源码:https://github.com/Feynman1999/CSDN ...

  6. ntpdate更新系统时间时报错Can't find host ntp1.aliyun.com: Servname not supported for ai_socktype (-8)

    ntpdate更新系统时间时报错Can't find host ntp1.aliyun.com: Servname not supported for ai_socktype (-8) 所报错误: [ ...

  7. Linux基础命令-02

    Linux基础命令-02:

  8. h5页面列表滚动加载数据

    h5列表滚动加载数据很常见,以下分享下今天做的案例: 前言 这个效果实现需要知道三个参数 1. scrollTop -- 滚动条距离顶部的高度 2. scrollHeight -- 当前页面的总高度( ...

  9. c++ 配置ffmpeg

    本教程只针对windows64/32+vs2013环境配置第一步 :配环境1.打开ffmpeg官网中编译好的windows版本http://ffmpeg.zeranoe.com/builds/64位w ...

  10. Arduino - -- 串口双向通信

    需要用到Arduino UNO的串口双向通信功能,以下源码: int val; void setup() {   Serial.begin(9600); // opensserial port, se ...