delphi+asp 找遍了整个百度都找不到我要的答案(200)

  • 主题发起人 主题发起人 sury8
  • 开始时间 开始时间
S

sury8

Unregistered / Unconfirmed
GUEST, unregistred user!
有一个asp程序,我现在要把我c盘根目录下的mm.gif图片上传到web服务器上,我想用delphi自动帮我告诉这个asp上传程序,告诉他file1是c:/mm.gif 一定要用delphi哦谢谢 各位<form name="upload" method="post" action="AddToDB.asp" enctype="multipart/form-data"><font color="red">请选择一个图象文件:</font><br />文件 : <input class="iFile" id="file1" type="file" name="file1" /> <br /><br /><input class="iButton" type="submit" value="开始上传" /></form>
 
简单啊:vars:tstringlist;oldp,tm,ct:word;begins.lines.loadfromfile(你的asp文件);for ct:=0 to s.lines.count-1 dobegin tm:=pos('type="file" name="',s[ct]);// 012345678901234567 if (tm<>0) then begin tm:=tm+17; oldp:=tm; while (s[ct][tm]<>'"') do tm:=tm+1; delete(s[ct],oldp,tm-oldp+1); insert(s[ct],oldp,'c:/mm.gif'); break; end;end;end;
 
给你转一段吧,GOOGLE中找到的PS:本人比较讨厌百度【Delphi】webbrowser控件自动填表模板Written on 2008年8月23日 @ 14:38 | by spirit | Tags: delphi 源码 WebBrowser | 浏览:1665 Delphi中利用webbrowser控件来实现自动填表,此例为一模板,稍作修改可用来自动申请QQ、邮箱、论坛ID之类(不包含验证码识别)。代码如下:unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,MSHTML, SHDOCVW,IdGlobal;type TMainFrm = class(TForm) btnTest: TButton; edURL: TEdit; Label1: TLabel; procedure btnTestClick(Sender: TObject); private { Private declarations } public { Public declarations } end;var MainFrm: TMainFrm;implementation{$R *.dfm}procedure FillIEForm(aURL:string); procedure DoWithHtmlElement(aElementCollection:IHTMLElementCollection); var k:integer; vk:oleVariant; Dispatch: IDispatch; HTMLInputElement:IHTMLInputElement; HTMLSelectElement:IHTMLSelectElement; HTMLOptionElement: IHTMLOptionElement; HTMLTextAreaElement: IHTMLTextAreaElement; HTMLFormElement:IHTMLFormElement; HTMLOptionButtonElement:IHTMLOptionButtonElement; begin for k:=0 to aElementCollection.length -1 do begin Vk:=k; Application.ProcessMessages; Dispatch:=aElementCollection.item(Vk,0); if Succeeded(Dispatch.QueryInterface(IHTMLInputElement,HTMLInputElement)) then begin With HTMLInputElement do//单行文本 begin if (UpperCase(Type_)='TEXT') or (UpperCase(Type_)='PASSWORD') then begin value:='text'; end else if (UpperCase(Type_)='CHECKBOX') then//复选框 begin checked:=true; end else if (UpperCase(Type_)='RADIO') then//单选框 begin checked :=true; end; end; end else if Succeeded(Dispatch.QueryInterface(IHTMLSelectElement,HTMLSelectElement)) then begin With HTMLSelectElement do//下拉框 begin selectedIndex :=1; end; end else if Succeeded(Dispatch.QueryInterface(IHTMLTEXTAreaElement,HTMLTextAreaElement)) then begin with HTMLTextAreaElement do//多行文本 begin value :='textarea'; end; end else if Succeeded(Dispatch.QueryInterface(IHTMLOptionElement,HTMLOptionElement)) then begin with HTMLOptionElement do//下拉选项 begin //处理 end; end else if SUCCEEDED(Dispatch.QueryInterface(IHTMLFormElement,HTMLFormElement))then begin with HTMLFormElement do//表单 begin //处理 end; end else if SUCCEEDED(Dispatch.QueryInterface(IHTMLOptionButtonElement,HTMLOptionButtonElement))then begin //不明 //处理 end else //showmessage('other'); ; end; end;var ShellWindow: IShellWindows; Web: IWebBrowser2; Dispatch: IDispatch; i,j:integer; IEAddress:string; HTMLDocument:IHTMLDocument2; ElementCollection:IHTMLElementCollection; FrameWindow:IHTMLWindow2; Vi,Vj:OLEVariant; HTMLFrameBase :IHTMLFrameBase ; HTMLFrameElement:IHTMLFrameElement ; HTMLIFrameElement:IHTMLIFrameElement;begin ShellWindow := CoShellWindows.Create; for i:=0 to ShellWindow.Count -1 do begin Vi:=i; Dispatch:=ShellWindow.Item(Vi); if Dispatch=nil then continue; Dispatch.QueryInterface(IWebBrowser2,Web); if Web<>nil then begin IEAddress:=Web.LocationURL; if Pos(aURL,IEAddress)>0 then begin Web.Document.QueryInterface(IHTMLDocument2,HTMLDocument); if HTMLDocument<>nil then begin if HTMLDocument.frames.length =0 then//无框架 begin ElementCollection:=HTMLDocument.Get_All; DoWithHtmlElement(ElementCollection); end else//有框架 begin for j:=0 to HTMLDocument.frames.length -1 do begin Vj:=j; Dispatch:=HTMLDocument.frames.item(Vj);// if Succeeded(Dispatch.QueryInterface(IHTMLFrameBase,HTMLFrameBase) if Succeeded(Dispatch.QueryInterface(IHTMLWindow2,FrameWindow)) then begin// DoWithHtmlElement(FrameWindow.document.all); end; End; end; end; end; End; end;end;procedure TMainFrm.btnTestClick(Sender: TObject);begin FillIEForm(edUrl.Text);end;end.本文来源于Spirit's Home http://www.7788sky.cn/ , 原文地址:http://www.7788sky.cn/post/delphi-webbrowser.html
 
2楼的兄弟 你也太搞笑了。是地球人都知道你的答案是错的
 
Delphi世界qq群:23981160喜欢d的都来
 
//-----以下是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中,必须在浏览器中选择文件,才能上载(号称是为了安全),通过这个就可以实现 不通过选择文件,实现上载。
 
可是死活提交不了我的文件 大家帮忙看看
 
考虑到好多人没有这个东西 我也找了一个星期 现在传成功了 代码贴出 有什么问题加我qq 1046496958 可以考虑整包发给你unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP;type TForm1 = class(TForm) Button1: TButton; IdHTTP1: TIdHTTP; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementationuses MsMultiPartFormData;{$R *.dfm}function HttpUpload(idHTTP:TIdHTTP;URL:String;FieldNames, FieldValues,FieldnFiles, FieldvFiles: array of string;ReturnValue:String='1'):Boolean;varresponseStream: TStringStream;mpfSource: TMsMultiPartFormDataStream;i:integer;n, v:String;beginresult:=false;mpfSource := TMsMultiPartFormDataStream.Create;responseStream := TStringStream.Create('');try idHTTP.Request.ContentType := mpfSource.RequestContentType; //解析字段名 for i := Low(FieldNames) to High(FieldNames) do begin n := FieldNames; v := FieldValues; mpfSource.AddFormField(n, v); end; //解析需要上传的文件名和文件地址 for i := Low(FieldnFiles) to High(FieldnFiles) do begin n := FieldnFiles; v := FieldvFiles; mpfSource.AddFile(n,v, 'Content-Type: image/gif'); end; mpfSource.PrepareStreamForDispatch; mpfSource.Position := 0; try idHTTP.Post(URL, mpfSource, responseStream); result:=returnvalue=trim(responseStream.DataString); except end;finally mpfSource.free; responseStream.free;end;end;procedure TForm1.Button1Click(Sender: TObject);constBaseURL = 'http://www.pubwin3000.cn/test/addtodb.asp'; //论坛所在地址varresponseStream: TStringStream;mpfSource: TMsMultiPartFormDataStream;a:String;beginmpfSource := TMsMultiPartFormDataStream.Create;responseStream := TStringStream.Create('');try IdHTTP1.Request.ContentType := mpfSource.RequestContentType; mpfSource.AddFormField('input1', '1046496958'); // mpfSource.AddFormField('resource', 'xxxx'); // mpfSource.AddFormField('file', 'C:/mm.jpg'); mpfSource.AddFile('file1','C:/8fff8.gif', 'Content-Type: image/gif'); mpfSource.PrepareStreamForDispatch; mpfSource.Position := 0; try IdHTTP1.Post(BaseURL, mpfSource, responseStream); //这里a是返回值,即页面上打出来的值 a:=trim(responseStream.DataString); showmessage(a); except end;finally mpfSource.free; responseStream.free;end;end;end.
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
I
回复
0
查看
811
import
I
I
回复
0
查看
757
import
I
后退
顶部