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 ...
随机推荐
- Vue - slot-scope="scope" 的意义
<template slot-scope="scope"> <el-button type="primary ...
- 聚类之K均值聚类和EM算法
这篇博客整理K均值聚类的内容,包括: 1.K均值聚类的原理: 2.初始类中心的选择和类别数K的确定: 3.K均值聚类和EM算法.高斯混合模型的关系. 一.K均值聚类的原理 K均值聚类(K-means) ...
- 155-PHP stripos函数
<?php $str='password'; //定义一个字符串 $position=strpos($str,'S'); //查找字母s第一次出现的位置 echo '字母S的位置是'.$posi ...
- 11 数据存储(Unity3D)
所有的游戏开发都离不开数据存储的操作,Unity3D也不例外PlayerPrefs:PlayerPrefs是Unity系统自带的的一种最简单的存储方式,数据是使用字典的方法来存储的 PlayerPre ...
- Docker NGINX 例子
版权所有,未经许可,禁止转载 章节 Docker 介绍 Docker 和虚拟机的区别 Docker 安装 Docker Hub Docker 镜像(image) Docker 容器(container ...
- 第十二篇视图层之视图函数(views)-三件套
视图层之视图函数(views) 阅读目录(Content) 视图层之视图函数(views) 一个简单的视图 HttpRequest HttpResponse redirect 函数 对比render与 ...
- 全面介绍Windows内存管理机制及C++内存分配实例
转自:http://blog.csdn.net/yeming81/article/details/2046193 本文基本上是windows via c/c++上的内容,笔记做得不错.. 本文背景: ...
- swift之水纹动画
import UIKit class CVLayerView: UIView { var pulseLayer : CAShapeLayer! //定义图层 override init(frame: ...
- core_cm4.h(129): error: #35: #error directive: "Compiler generates FPU instructions for a device without an FPU (check __FPU_PRESENT)"
今天使用 systick 的时候,只使用了头文件 core_cm4.h,结果就报错了,原因是 __FPU_PRESENT 没有定义,这个定义其实在 stm32f4xx.h 里面.所以如果要解决这个错误 ...
- 吴裕雄--天生自然 JAVASCRIPT开发学习:语句
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...