delphi,c#,java,php,mysql,入门,教程,电子书

c-sharp,sql,.net,编程,数据库,visual studio,j2ee,免费,控件,下载,电子书,手册

delphi中通过TIdhttp控件上载文件

附带的例子由于indy版本的问题,有点小问题(其实用文本工具修改一下dfm,去掉新增的属性就可以了)。我干脆把源码给贴出来吧,版权属于原作者
一.用于实现form流格式的单元(MsMultiPartFormData.pas)
unit MsMultiPartFormData;
interface
uses
SysUtils, Classes;
const
CONTENT_TYPE = 'multipart/form-data; boundary=';
CRLF = #13#10;
CONTENT_DISPOSITION = 'Content-Disposition: form-data; name="%s"';
FILE_NAME_PLACE_HOLDER = '; filename="%s"';
CONTENT_TYPE_PLACE_HOLDER = 'Content-Type: %s' + crlf + crlf;
CONTENT_LENGTH = 'Content-Length: %d' + crlf;
type
TMsMultiPartFormDataStream = class(TMemoryStream)
private
FBoundary: string;
FRequestContentType: string;
function GenerateUniqueBoundary: string;
public
procedure AddFormField(const FieldName, FieldValue: string);
procedure AddFile(const FieldName, FileName, ContentType: string; FileData: TStream); overload;
procedure AddFile(const FieldName, FileName, ContentType: string); overload;
procedure PrepareStreamForDispatch;
constructor Create;
property Boundary: string read FBoundary;
property RequestContentType: string read FRequestContentType;
end;
implementation
{ TMsMultiPartFormDataStream }
constructor TMsMultiPartFormDataStream.Create;
begin
inherited;
FBoundary := GenerateUniqueBoundary;
FRequestContentType := CONTENT_TYPE + FBoundary;
end;
procedure TMsMultiPartFormDataStream.AddFile(const FieldName, FileName,
ContentType: string; FileData: TStream);
var
sFormFieldInfo: string;
Buffer: PChar;
iSize: Int64;
begin
iSize := FileData.Size;
sFormFieldInfo := Format(CRLF + '--' + Boundary + CRLF + CONTENT_DISPOSITION +
FILE_NAME_PLACE_HOLDER + CRLF + CONTENT_LENGTH +
CONTENT_TYPE_PLACE_HOLDER, [FieldName, FileName, iSize, ContentType]);
Write(Pointer(sFormFieldInfo)^, Length(sFormFieldInfo));
FileData.Position := 0;
GetMem(Buffer, iSize);
try
FileData.Read(Buffer^, iSize);
Write(Buffer^, iSize);
finally
FreeMem(Buffer, iSize);
end;
end;
procedure TMsMultiPartFormDataStream.AddFile(const FieldName, FileName,
ContentType: string);
var
FileStream: TFileStream;
begin
FileStream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
AddFile(FieldName, FileName, ContentType, FileStream);
finally
FileStream.Free;
end;
end;
procedure TMsMultiPartFormDataStream.AddFormField(const FieldName,
FieldValue: string);
var
sFormFieldInfo: string;
begin
sFormFieldInfo := Format(CRLF + '--' + Boundary + CRLF + CONTENT_DISPOSITION + CRLF + CRLF +
FieldValue, [FieldName]);
Write(Pointer(sFormFieldInfo)^, Length(sFormFieldInfo));
end;
function TMsMultiPartFormDataStream.GenerateUniqueBoundary: string;
begin
Result := '---------------------------' + FormatDateTime('mmddyyhhnnsszzz', Now);
end;
procedure TMsMultiPartFormDataStream.PrepareStreamForDispatch;
var
sFormFieldInfo: string;
begin
sFormFieldInfo := CRLF + '--' + Boundary + '--' + CRLF;
Write(Pointer(sFormFieldInfo)^, Length(sFormFieldInfo));
Position := 0;
end;
end.
二。调用的方法:
1。先包含MsMultiPartFormData(uses MsMultiPartFormData;)
2。把如下代码加到需要的地方
var
ResponseStream: TMemoryStream;
MultiPartFormDataStream: TMsMultiPartFormDataStream;
begin
MultiPartFormDataStream := TMsMultiPartFormDataStream.Create;
ResponseStream := TMemoryStream.Create;
try
IdHttp1.Request.ContentType := MultiPartFormDataStream.RequestContentType;
//添加表单的字段 (前一个参数是字段名,后一个参数是字段值)
MultiPartFormDataStream.AddFormField('PersonName', edtPersonName.Text);
MultiPartFormDataStream.AddFormField('Description', edtDescription.Text);
//添加上载的文件(第一个是字段名,第二个是文件名,第三个是文件类型)
MultiPartFormDataStream.AddFile(edtFile.Name, edtFile.Text, edtMIMEType.Text);
{ You must make sure you call this method *before* sending the stream }
MultiPartFormDataStream.PrepareStreamForDispatch;
MultiPartFormDataStream.Position := 0;
//调用idhttp的post方法,第一个参数是用于处理上载form的asp/php等等脚本,第三个是接收脚本执行完成后的返回内容)
IdHTTP1.Post("http://xxx.xxx.com/upload.asp", MultiPartFormDataStream, ResponseStream);
finally
MultiPartFormDataStream.Free;
ResponseStream.Free;
end;
end;
从这些代码可以引出很多应用:
1。asp里调用其它脚本语言如(php,jsp,等等)。把这段代码用组件实现,在asp中调用,就可以 了。
2。从普通的应用程序调用asp,php等等脚本
3。传统的html中,必须在浏览器中选择文件,才能上载(号称是为了安全),通过这个就可以实现
不通过选择文件,实现上载。 
阅读更多
个人分类: delphi
上一篇数据库范式
下一篇怎么样得到文件的大小?(DELPHI)
想对作者说点什么? 我来说一句

delphi 上传文件至web服务器

2009年02月22日 232KB 下载

没有更多推荐了,返回首页

关闭
关闭