idtcp实现文件下载和上传
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实现文件下载和上传的更多相关文章
- Pikachu漏洞练习平台实验——不安全的文件下载和上传(七)
1.不安全的文件下载 1.1.概述 文件下载功能在很多web系统上都会出现,一般我们当点击下载链接,便会向后台发送一个下载请求,一般这个请求会包含一个需要下载的文件名称,后台在收到请求后 会开始执行下 ...
- pikachu-不安全的文件下载和上传
一.文件下载漏洞 1.1 概述 很多网站都会提供文件下载的功能,即用户可以通过点击下载链接,下载到链接所对应的文件.但是,如果文件下载功能设计不当,则可能导致攻击者可以通过构造文件路径,从而获 ...
- day58:Linux:BashShell&linux文件管理&linux文件下载上传
目录 1.BashShell 2.Linux文件管理 3.Linux文件下载和上传 BashShell 1.什么是BeshShell? 命令的解释,用来翻译用户输入的指令 2.BashShell能做什 ...
- [实战]MVC5+EF6+MySql企业网盘实战(12)——新建文件夹和上传文件
写在前面 之前的上传文件的功能,只能上传到根目录,前两篇文章实现了新建文件夹的功能,则这里对上传文件的功能进行适配. 系列文章 [EF]vs15+ef6+mysql code first方式 [实战] ...
- web开发实战--图片裁剪和上传
前言: 最近的开发中, 有一个上传头像的任务. 由于头像本身的特殊性, 其一般流程为选择图片, 编辑裁剪区域, 再继而上传图片操作. 看似简单的东西, 实则是挺麻烦的一件事. 借助这次开发机会, 来具 ...
- 基于uploadify.js实现多文件上传和上传进度条的显示
uploadify是JQuery的一个插件,主要实现文件的异步上传功能,可以自定义文件大小限制.文件类型.是否自动上传等属性,可以显示上传的进度条.官网地址是http://www.uploadify. ...
- 重新想象 Windows 8 Store Apps (66) - 后台任务: 下载和上传
[源码下载] 重新想象 Windows 8 Store Apps (66) - 后台任务: 下载和上传 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 后台任务 后台 ...
- 重新想象 Windows 8.1 Store Apps (91) - 后台任务的新特性: 下载和上传的新特性, 程序启动前预下载网络资源, 后台任务的其它新特性
[源码下载] 重新想象 Windows 8.1 Store Apps (91) - 后台任务的新特性: 下载和上传的新特性, 程序启动前预下载网络资源, 后台任务的其它新特性 作者:webabcd 介 ...
- div+css网页本地上和上传到服务器后在IE11上看到的效果不一样?
div+css网页本地上和上传到服务器后在IE11上看到的效果不一样? 解决办法在html的head里加上一段:<meta http-equiv="X-UA-Compatible&qu ...
随机推荐
- Express - 采用bcryptjs进行密码加密
1.安装bcryptjs模块 npm install bcryptjs --save 2.在需要加密的模块中引入bcryptjs库 require('bcryptjs'); 实战 : /** ...
- Python+Selenium中级篇之8-Python自定义封装一个简单的Log类《转载》
Python+Selenium中级篇之8-Python自定义封装一个简单的Log类: https://blog.csdn.net/u011541946/article/details/70198676
- 【转】R函数-diag()函数
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/lili_wuwu/article/det ...
- 七十七、SAP中数据库操作之多表联合查询
一.我们看一下SFLIGHT表和SPFLI表,表结构如下 二.这2个表的数据如下 三.我们代码如下 四.多表联合查询结果如下
- 016-PHP读取文件常见属性
<?php print("文件的所有者(UID 值):"); print(fileowner("data.txt") . "<br> ...
- PhotoView 实现与图片进行简单的交互
本文的category是根据VIPhotoView来做参考,在此基础上添加个加载网络图片. 此category主要功能是与图片进行交互,双击放大图片,捏合等操作. 感谢vitoziv ! VIPhot ...
- P 1019 数字黑洞
转跳点:
- 17. react redux的中间件
1. redux 数据流程图 View 会派发一个 Action Action 通过 Dispatch 方法派发给 Store Store 接收到 Action 连同之前的 State 发给 Red ...
- 三十、CI框架之使用cookies实现用户登录和退出。
一.在控制器中,写入3个函数.一个是login用于登录,一个是logout用于退出,一个show用来展示界面. login代码如下: logout和showuser函数如下: 二.我们的登录界面 三. ...
- oracle数据删除恢复
insert into 表名 select * from 表名 as of timestamp to_Date('2017-07-20 10:00:00', 'yyyy-mm-dd hh24:mi:s ...