cancel
Showing results for 
Search instead for 
Did you mean: 

Upload File Programetically Using OLE Object to WebBrowser

Former Member
0 Kudos

Hi All.

I want to upload file authenticated website using OLE method from PowerBuilder to WebBrowser. Below is my step which i have tried.

I am able to navigate the url, login and go to particular URL. Now I have struck at file upload level.

Variable type is coming as 'file' [Red  Marked Line ], how to pass file path and name from PB. Below is my script

IE = CREATE OLEObject
IE.ConnectToNewObject("InternetExplorer.Application")

IE.left=200
IE.top=200
IE.height=400
IE.width=400
IE.menubar=1
IE.toolbar=1
IE.statusBar=1
IE.navigate("http://www.xyz.in/index.php")

  1. IE.visible=1
    SetForegroundWindow( IE.HWND )

    DO WHILE IE.Busy
    Yield ()
    LOOP

li_form_ctr = IE.Document.Forms.Length
if li_form_ctr = 1 then
li_form_ctr = li_form_ctr - 1
IE.Document.Forms[li_form_ctr].Elements.username.Value = 'Test'
IE.Document.Forms[li_form_ctr].Elements.password.Value = 'test@123'
ls_OldURL = IE.LocationURL
IE.Document.Forms[li_form_ctr].Elements.submit.Click()
else
return
end if  
DO WHILE IE.Busy
Yield ()
LOOP
if ls_OldURL = IE.LocationURL then
messagebox("Error","Either User Name/Password Wrong")
else
IE.navigate("http://www.xyz.in/upload_ecr_latest.php")
DO WHILE IE.Busy
Yield ()
LOOP
sleep(2)
li_form_ctr = 0
li_form_ctr = IE.Document.Forms.Length
if li_form_ctr = 1 then
li_form_ctr = li_form_ctr - 1
  IE.Document.Forms[li_form_ctr].Elements.month.Value = '08'
  IE.Document.Forms[li_form_ctr].Elements.year.Value = '2014'
IE.Document.Forms[li_form_ctr].Elements.ecr = "D:\temp\xyz.txt" -- This one is not working
ls_OldURL = IE.LocationURL
IE.Document.Forms[li_form_ctr].Elements.upload.Click()
else
return
end if      
end if 

Narayana

Accepted Solutions (0)

Answers (1)

Answers (1)

arnd_schmidt
Active Contributor
0 Kudos

AFAIK, you can't set the value for input type =  "file" by script because of security reasons.

Arnd

Former Member
0 Kudos

Hi Schmidt,

I Know that [i came to know from google search], but what is the alternate method or script? I am unable get alternate script for the same.

Narayana

Former Member
0 Kudos

I have a sample that uses WinHTTP API functions to support HTTP Put. It is similar to the built in PostURL function except it supports secure URLs and is easier to use.

Topwiz Software - WinHTTP

Former Member
0 Kudos

Dear Roland,

Thanks for the quick reply.

I have tried WinHTTP and i am getting "HTTP Post Error #12002". Please find  Below modified script. I have created function for below script and calling in place of file upload.

n_winhttp ln_http

String  ls_filename,ls_path_name

String ls_filter, ls_mimetype, ls_data

Integer li_rc, li_fnum

ULong lul_length

ls_filename         = 'xyz.txt'

ls_path_name  = 'D:\temp\xyz.txt'

li_fnum = FileOpen(ls_path_name, TextMode!, Read!)

lul_length = FileReadEx(li_fnum, ls_data)

FileClose(li_fnum)

SetPointer(HourGlass!)

ln_http.SetWriteProgress(Handle(this), 1024)

ls_URL  = "http://www.xyz.in/upload_ecr_latest.php?filename="+ ls_filename

//ls_URL += "/winhttp/roland_post.asp?filename=" + ls_filename

ls_mimetype = ln_http.GetMIMEType(ls_filename, ls_data)

ln_http.Open("POST", ls_URL)

ln_http.SetRequestHeader("Content-Length", String(lul_length))

ln_http.SetRequestHeader("Content-Type", ls_mimetype)

lul_length = ln_http.Send(ls_data)

If lul_length > 0 Then

    mle_response.text = ln_http.ResponseText

    st_msg.text = "Post Complete in " + &

        String(ln_http.Elapsed, "#,##0.####") + " seconds."

Else

    MessageBox("HTTP Post Error #" + &

                    String(ln_http.LastErrorNum), &

                    ln_http.LastErrorText, StopSign!)

End If

Narayana

Former Member
0 Kudos

Error 12002 is: The request has timed out.

It could be an issue with your script or maybe write permissions on the website. I included a file post.asp that can be used to test with. It is Classic ASP (vbscript). You could try posting to it instead. First try it with the stream functions commented out so all it does is return the number of bytes received.

Former Member
0 Kudos

Dear Roland,

Thanks for the Speedy reply.

I have searched post.asp in winhttp.zip, but i can't find that? can you tell me where you have included post.asp?

Narayana

Former Member
0 Kudos

I guess I forgot to include it. Save the below text to a file called post.asp and upload to your website.

<%

Response.ContentType="text"

set stream=Server.CreateObject("ADODB.Stream")

filename = Server.MapPath(Request("filename"))

count  = Request.TotalBytes

buffer = Request.BinaryRead(count)

stream.Open

stream.Type=1    ' 1=Binary, 2=Text (Default)

stream.Write buffer

stream.SaveToFile filename

stream.Close

Response.Write "Bytes Received: " & count & ", Filename: " & filename

%>

Former Member
0 Kudos

Dear Roland,

Thank you for the reply

Merry Christmas!!!

I have uploaded post.asp to my local website and it is working fine using post.asp. But i am trying to upload file to Government statutory website which fails. Even-though i am go-ogling and trying alternative method but no success. If you find any method please update me, thanks in advance

Narayana