Script Recording with HTML File Field
Brian Donahue
Posts: 6,590 Bronze 1
Hi Scott,
Thanks for pointing this out. What ANTS Load looks like it's doing is
posting nothing back to the server. Here is an example of the resulting
script that I got back after trying to record a session where I try to
upload a file to the web server:
WebClient.GET(http://thing/Aardvark/styles/main.css)
WebClient.GET("http://thing/Aardvark/images/buttons/upload.gif")
WebClient.GET("http://thing/Aardvark/images/buttons/closewindow.gif")
Sleep(20249)
BeginPage("http://thing/Aardvark/attachments.asp")
WebClient.HttpRequest.Pragma = "no-cache"
WebClient.HttpRequest.ContentType = "multipart/form-data;
boundary=
7d42b922b08fe"
WebClient.HttpRequest.AddPostData("")
WebClient.POST(http://thing/Aardvark/attachments.asp)
EndPage(http://thing/Aardvark/attachments.asp)
It doesn't look like ANTS Load can cope with this. For uploading a file,
there is an undocumented method called SetPostData. I think for simulating a
file upload, you would have to do it by hand using the SetPostData method
like this:
WebClient.GET("http://thing/Aardvark/styles/main.css")
WebClient.GET("http://thing/Aardvark/images/buttons/upload.gif")
WebClient.GET("http://thing/Aardvark/images/buttons/closewindow.gif")
Sleep(20249)
BeginPage("http://thing/Aardvark/attachments.asp")
WebClient.HttpRequest.Pragma = "no-cache"
WebClient.HttpRequest.ContentType = "application/octet-stream"
WebClient.HttpRequest.SetPostData(ReadFileAsBytes("c: empwidget.txt"))
WebClient.POST("http://thing/Aardvark/attachments.asp")
EndPage("http://thing/Aardvark/attachments.asp")
I haven't tested this, but you could put a function in your script that
converts the file that you want to upload into a byte array:
Private Function ReadFileAsBytes(ByVal filename As String) As Byte()
Dim fs As New FileStream(filename, FileMode.Open)
Dim byteArray As Byte()
fs.Read(byteArray, 0, Convert.ToInt32(fs.Length))
ReadFileAsBytes = byteArray
End Function
Hopefully this will work for you.
Regards,
Brian Donahue
Red Gate Technical Support
"Scott [C# MVP]" <s@s.s> wrote in message news:PHoBP1XiEHA.1204@server53...
> Greetings,
>
> I have a web form that has an HTML File Field control on it. When I try
to
> record a script using this page, the request never seems to resolve. I
can
> browse for the file using the control's "Browse" button, but when I click
> the submit button on the form, the request just goes on and on and on.
>
> Any ideas?
>
>
> -Scott
>
>
Thanks for pointing this out. What ANTS Load looks like it's doing is
posting nothing back to the server. Here is an example of the resulting
script that I got back after trying to record a session where I try to
upload a file to the web server:
WebClient.GET(http://thing/Aardvark/styles/main.css)
WebClient.GET("http://thing/Aardvark/images/buttons/upload.gif")
WebClient.GET("http://thing/Aardvark/images/buttons/closewindow.gif")
Sleep(20249)
BeginPage("http://thing/Aardvark/attachments.asp")
WebClient.HttpRequest.Pragma = "no-cache"
WebClient.HttpRequest.ContentType = "multipart/form-data;
boundary=
7d42b922b08fe"
WebClient.HttpRequest.AddPostData("")
WebClient.POST(http://thing/Aardvark/attachments.asp)
EndPage(http://thing/Aardvark/attachments.asp)
It doesn't look like ANTS Load can cope with this. For uploading a file,
there is an undocumented method called SetPostData. I think for simulating a
file upload, you would have to do it by hand using the SetPostData method
like this:
WebClient.GET("http://thing/Aardvark/styles/main.css")
WebClient.GET("http://thing/Aardvark/images/buttons/upload.gif")
WebClient.GET("http://thing/Aardvark/images/buttons/closewindow.gif")
Sleep(20249)
BeginPage("http://thing/Aardvark/attachments.asp")
WebClient.HttpRequest.Pragma = "no-cache"
WebClient.HttpRequest.ContentType = "application/octet-stream"
WebClient.HttpRequest.SetPostData(ReadFileAsBytes("c: empwidget.txt"))
WebClient.POST("http://thing/Aardvark/attachments.asp")
EndPage("http://thing/Aardvark/attachments.asp")
I haven't tested this, but you could put a function in your script that
converts the file that you want to upload into a byte array:
Private Function ReadFileAsBytes(ByVal filename As String) As Byte()
Dim fs As New FileStream(filename, FileMode.Open)
Dim byteArray As Byte()
fs.Read(byteArray, 0, Convert.ToInt32(fs.Length))
ReadFileAsBytes = byteArray
End Function
Hopefully this will work for you.
Regards,
Brian Donahue
Red Gate Technical Support
"Scott [C# MVP]" <s@s.s> wrote in message news:PHoBP1XiEHA.1204@server53...
> Greetings,
>
> I have a web form that has an HTML File Field control on it. When I try
to
> record a script using this page, the request never seems to resolve. I
can
> browse for the file using the control's "Browse" button, but when I click
> the submit button on the form, the request just goes on and on and on.
>
> Any ideas?
>
>
> -Scott
>
>
This discussion has been closed.
Comments
I've had a crack at the file upload again and have found it is not as
simple as it should be.
Here is the script that I came up with after a bit of playing around:
Sleep(20249)
Dim fileBytes As Byte()
fileBytes = ReadFileAsBytes("c: empwidgets.txt")
BeginPage("http://thing/Aardvark/attachments.asp")
WebClient.HttpRequest.Pragma = "no-cache"
WebClient.HttpRequest.Headers.Add("Content-Type", "multipart/form-data")
WebClient.HttpRequest.Headers.Add("Content-Disposition", "form-data;
name=""file""; filename=""widgets.txt""")
WebClient.HttpRequest.SetPostData(fileBytes)
WebClient.HttpRequest.ContentType = "text/plain"
Me.LogToFile(WebClient.HttpRequest.ToString())
WebClient.Post("http://thing/Aardvark/attachments.asp")
EndPage(http://thing/Aardvark/attachments.asp)
There is still a problem in that I can't specify the boundaries that I
need to specify with multipart/form data. The request, in its raw form,
should look like this:
Content-Type: multipart/form-data; boundary=AaB03x
--AaB03x
Content-Disposition: form-data; name="file"; filename="widgets.txt"
Content-Type: text/plain
... contents of widgets.txt ...
--AaB03x--
**Note in this asp page, there is only one control called file and the rest
of the information that the page needs to properly store the file is held in
the session. **
But the closest I can get is this, which I've got by logging the
HttpRequest.ToString():
Pragma: no-cache
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR
1.1.4322; .NET CLR 1.0.3705)
Host: thing
Content-Disposition: form-data; name="file"; filename="widgets.txt"
Content-Type: text/plain
Cookie: AutoLogon=; Remember=off; Company=; User=;
ASPSESSIONIDAQRBTQQT=GKIFPPHDMLMOEOOOHFFALJDK
Content-Length: 27
abcdefghijklmnopqrstuvwxyz
I'll keep working on this until I can figure out a way to get it to
work.
"Scott [C# MVP]" <s@s.s> wrote in message news:PHoBP1XiEHA.1204@server53...
> Greetings,
>
> I have a web form that has an HTML File Field control on it. When I try
to
> record a script using this page, the request never seems to resolve. I
can
> browse for the file using the control's "Browse" button, but when I click
> the submit button on the form, the request just goes on and on and on.
>
> Any ideas?
>
>
> -Scott
>
>
I've finally had success simulating a file upload. The way to do it is
to manually construct a multipart form post and send it to the server using
the SetPostData method. The full procedure is detailed in the attached Word
document.
Regards,
Brian Donahue
Red Gate Technical Support
"Scott [C# MVP]" <s@s.s> wrote in message news:PHoBP1XiEHA.1204@server53...
> Greetings,
>
> I have a web form that has an HTML File Field control on it. When I try
to
> record a script using this page, the request never seems to resolve. I
can
> browse for the file using the control's "Browse" button, but when I click
> the submit button on the form, the request just goes on and on and on.
>
> Any ideas?
>
>
> -Scott
>
>
The attachment was available on our old newsgroup server, but the forums do not support attachments. If you'd like a copy of the instructions for uploading files in ANTS Load test scripts, please write us at support@red-gate.com.
Additionally, we have written a small add-in that will construct a multipart post containing the contents of a file from the local hard disk.