//-----以下是Delphi代码-----Seeker的Delphi编程小站-----// 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("<a href="http://www.QQView.com/upload.asp" target="_blank" rel="external">http://www.QQView.com/upload.asp</a>", MultiPartFormDataStream, ResponseStream); finally MultiPartFormDataStream.Free; ResponseStream.Free; end; end; 从这些代码可以引出很多应用: 1。asp里调用其它脚本语言如(php,jsp,等等)。把这段代码用组件实现,在asp中调用,就可以 了。 2。从普通的应用程序调用asp,php等等脚本 3。传统的html中,必须在浏览器中选择文件,才能上载(号称是为了安全),通过这个就可以实现 不通过选择文件,实现上载。