请问,如何填写TWebbrowser中的‘file’类文本框? (50分)

D

demonP

Unregistered / Unconfirmed
GUEST, unregistred user!
对于网页上文件类型的输入元素,该怎么将文件名填进去呢?
例如,如下的一个元件,
<input type="file" size=30 name=file0 value="*.jpg">
因为value是只读的,所以不能直接赋值。那有什么变通的办法么?


请指点一下,谢谢了!
 
file 的 value 是只读的!!!!!!!无法用程序改变.

Retrieves the file name of the input object after the text is set by user input.

Syntax

HTML N/A
Scripting [ sValue = ] INPUT type=file.value

Possible Values

sValue String that receives the file name.

The property is read-only. The property has no default value.
////////
Remarks

The value property returns only the file name and not the path of the file to machines outside the local machine security zone. For more information on security zones, see About URL Security Zones.



点击"浏览"后,可以用以下程序显示,但无法改变!!!!!!!!
uses ... mshtml;

procedure TForm1.FormCreate(Sender: TObject);
begin
webbrowser1.Navigate('d:/tt.htm');
end;

procedure TForm1.Button1Click(Sender: TObject);
var
doc:IHTMLdocument2;
v:variant;
begin
doc:=IHTMLdocument2(webbrowser1.document);
v:=doc.all.item('file0',0);
showmessage(v.value);
v.value:='c:/windows/notepad.exe' ;
showmessage(v.value);
end;



tt.htm


<HTML>
<HEAD>
<TITLE> New Document </TITLE>
</HEAD>

<BODY>
<input type="file" size=30 id=file0 value="*.jpg">

</BODY>
</HTML>
 
原来是这样!
其实我是想实现批量文件上传功能,不知该怎么实现呢?
 
file 的 value 是只读的!!!!!!!无法用程序改变.
我这里怎么text 的也不能改变呢????
 
用我的代码text类可以改变的。
 
实在想不出办法,所以就移到“非技术区”,大家聊聊吧。
我刚才有个想法,可以把focus移到那个edit上,然后把文件名
用消息的方式传上去,但后来一想不太可行,如果我的应用
退到后台失去焦点后,靠发消息还能不能把字符写进edit呢?
而且接受消息的窗口句柄我还不太清楚怎么得到,如果用WEBBROWSER.HWND
会发生“未定义的错误”,大家怎么看?
 
以下方法可以实现,但特殊字符要转换一下:

procedure TForm1.FormCreate(Sender: TObject);
begin
webbrowser1.Navigate('d:/tt.htm');
end;

procedure TForm1.Button1Click(Sender: TObject);
var
doc:IHTMLdocument2;
v:variant;
x,y:integer;
p:TPoint;
s:string;
begin
s:='MYFILE.EXT';//要大写,特殊符号要处理
doc:=IHTMLdocument2(webbrowser1.document);
v:=doc.all.item('file0',0);
activecontrol:=webbrowser1;
webbrowser1.SetFocus ;
v.select;//不起作用,有没有其它方法
////// 实现 select
y:=v.offsetTop+webbrowser1.top+5;
x:=v.offsetLeft+webbrowser1.Left+5;
p:=clienttoscreen(point(x,y));
setcursorpos(p.x,p.y);
mouse_event(MOUSEEVENTF_LEFTDOWN ,0,0,0,0);
mouse_event(MOUSEEVENTF_LEFTUP ,0,0,0,0);
////// 实现 select
for x:=1 to length(s) do
begin
y:=ord(s[x]);
if y=ord('.') then y:=vk_Decimal;
keybd_event(y,0,0,0);
keybd_event(y,0,KEYEVENTF_KEYUP,0);
end;
end;
 
谢谢jsxjd,我是用下面的代码实现这个方法的,
但就像我上面写的,如果我的应用在前台,
这段代码工作的很好,但如果应用失去了焦点,
它就不起作用了,原因就在于页面上的元件不是
窗口,它们没有自己的句柄和处理过程,而对普通
的窗口元素就没这个问题。
...
var
i:integer;
o: OleVariant;
begin
HtmlDoc := Webbrowser1.Document;
if not VarIsClear(HtmlDoc) then
o:= Htmldoc.all.item('file0',0) ;
if not VarIsClear(o) then
begin
o.focus; //《--用这一句就可以实现select了!
keybd_event(Ord('S'),0,0,0);
end;
 
或许可以每次填写文件名之前,强行把应用的窗口提到前台,获得焦点,
但这样的应用实在是不太“礼貌”吧。
 
下面的东东是我这两天的成果,基本可以实现yahoo 公文包的
无人值守批量上传,但毛病实在太多,哪位大侠有兴趣改的话
千万通知我一声。
最后谢谢回答问题的两位朋友。

function TForm1.Submit:boolean;
var
i:integer;
CurrentFile:string;
BrowserHwnd:HWnd;
begin
result := false;
HtmlDoc := Webbrowser1.Document;
if not VarIsClear(HtmlDoc) then
o:= Htmldoc.all.item('file0',0)
else
begin
ShowMessage('Html.Document Error!');
exit;
end;
if not VarIsClear(o) then
begin
if ListView1.Items.Count > 0 then
begin
UploadURL := WebBrowser1.LocationURL;
CurrentFile := ListView1.Items[fileindex].Caption;
SetForegroundWindow(form1.Handle);
for i:=1 to Length(CurrentFile) do
begin
o.focus;
BrowserHwnd := GetWindow(Webbrowser1.Handle,GW_CHILD);
SendMessage(GetWindow(BrowserHwnd,GW_CHILD),WM_CHAR,ord(CurrentFile),$00200001);
end;
o :=HtmlDoc.all.item('.Upload',0);
o.click;
end
else
begin
ShowMessage('No file selected!');
exit;
end;
end
else
begin
ShowMessage('"file0" object not found!');
exit;
end;
result := true;
end;

procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
begin
if not StopFlag then
begin
HtmlDoc := Webbrowser1.Document;
if HtmlDoc.title = 'Yahoo! Briefcase - Confirm Upload Files' then
begin
Webbrowser1.Navigate(UploadURL);
ListView1.items[0].Delete;
end;
if not VarisClear(HtmlDoc.all.item('file0',0)) then
if ListView1.Items.Count <>0 then
Submit
else
begin
StopFlag := true;
show;
ShowMessage('Upload Finished !');
end;

end;
end;
 
顶部