Library tutorials & articles

Interacting with the web using WebRobot v1.0

Uploading files to YouSendIt: Setting form values

Now that we have read our file into a System.IO.MemoryStream, we can populate our form fields with data:

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
    Dim wrobot As New foxtrot.xray.WebRobot
    wrobot.Base = "http://www.yousendit.com/"
    wrobot.LoadPage("/")
    'getting file upload form from page
    Dim wform As foxtrot.xray.Form = wrobot.GetFormByName("tform")
    wform.AddField("text", "rcpt")
    wform.AddField("file", "fname")
    wform.AddField("hidden", "rurl", _
"http://www.yousendit.com/transfer.php?action=status")
    'creating an Open File Dialog to choose file to upload
    Dim fopendialog As New System.Windows.Forms.OpenFileDialog
    fopendialog.CheckFileExists = True
    If fopendialog.ShowDialog() = DialogResult.OK Then
        'open file for reading
        Dim fstream As New System.IO.FileStream(fopendialog.FileName, _
IO.FileMode.Open)
'temporary buffer to store contents of file
Dim bytFile(fstream.Length - 1) As Byte
        fstream.Read(bytFile, 0, fstream.Length) 'read in the file
        'the contents of the file to be uploaded to the server
        Dim mstream As New System.IO.MemoryStream(bytFile)
        'getting email field by name from form
        Dim wemail As foxtrot.xray.Input = wform.GetFieldByName("rcpt")
        'getting file upload field from form
        Dim wfile As foxtrot.xray.Input = wform.GetFieldByName("fname")
        'set your recipient's email here
   wemail.InputValue = "none@domain.com"
   'assign contents of file to the form field
   wfile.InputValue = mstream
   'assign filename to be sent
   wfile.InputFileName = fopendialog.FileName
    End If
End Sub
YouSendIt uses JavaScript extensively to manipulate the form's contents, so before we submit the form, we must find a JavaScript variable called PROGRESS_ID, and append the value of the variable to the form action. So, if our form had an action "http://ftf-91.yousendit.com/upload", the JavaScript code sets the form action to "http://ftf-91.yousendit.com/upload/9999999", where the trailing number is contained in the PROGRESS_ID variable. Let's find the variable and append its value to the form action:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim wrobot As New foxtrot.xray.WebRobot
    wrobot.Base = "http://www.yousendit.com/"
    wrobot.LoadPage("/")
    'getting file upload form from page
    Dim wform As foxtrot.xray.Form = wrobot.GetFormByName("tform")
    wform.AddField("text", "rcpt")
    wform.AddField("file", "fname")
    wform.AddField("hidden", "rurl", "http://www.yousendit.com/transfer.php?action=status")
    'creating an Open File Dialog to choose file to upload
    Dim fopendialog As New System.Windows.Forms.OpenFileDialog
    fopendialog.CheckFileExists = True
    If fopendialog.ShowDialog() = DialogResult.OK Then
        'open file for reading
        Dim fstream As New System.IO.FileStream(fopendialog.FileName, IO.FileMode.Open)
        Dim bytFile(fstream.Length - 1) As Byte 'temporary buffer to store contents of file
        fstream.Read(bytFile, 0, fstream.Length) 'read in the file
        'the contents of the file to be uploaded to the server
        Dim mstream As New System.IO.MemoryStream(bytFile)
        'getting email field by name from form
        Dim wemail As foxtrot.xray.Input = wform.GetFieldByName("rcpt")
       'getting file upload field from form
        Dim wfile As foxtrot.xray.Input = wform.GetFieldByName("fname")
        wemail.InputValue = "none@domain.com" 'set your recipient's email here
        wfile.InputValue = mstream 'assign contents of file to the form field
        wfile.InputFileName = fopendialog.FileName 'assign filename to be sent
        Dim progressid As String
        'regular expression to find the PROGRESS_ID variable in the HTML source
        Dim regex As New System.Text.RegularExpressions.Regex_ ("var\sPROGRESS_ID\s=\s""(?<progid>\d+)"";")
        If (regex.IsMatch(wrobot.HTMLSource) = True) Then
            progressid = regex.Match(wrobot.HTMLSource).Result("${progid}")
            wform.FormAction = wform.FormAction & "/" & progressid 'modifying the form action
        Else
            MessageBox.Show("YouSendIt PROGRESS_ID variable not found, cannot continue with upload!")
        End If
    End If
End Sub

Comments

  1. 18 Jun 2007 at 14:59
    Yes it'll save a plenty of work, thanks .
  2. 11 Apr 2007 at 14:07
    dinuX wrote:

    This is a Very Good Example.I thin its very usefull to me.

    but the problem is when i try this this not work properly error occured at this line  "Dim wrobot As New foxtrot.xray.WebRobot()"

    it displayes "File or Assembly name System,or one of its dependancies was not found."

     

    Please answer to me

    did you make the reference to the package?
  3. 11 Apr 2007 at 14:05
    That looks very nice. It'll save me plenty of work when posting a form instead of hardcoding all the fields in a webrequest! http://www.fixx.be
  4. 04 Oct 2006 at 10:52

    This is a Very Good Example.I thin its very usefull to me.

    but the problem is when i try this this not work properly error occured at this line  "Dim wrobot As New foxtrot.xray.WebRobot()"

    it displayes "File or Assembly name System,or one of its dependancies was not found."

     

    Please answer to me.

     

     

  5. 01 Jan 1999 at 00:00

    This thread is for discussions of Interacting with the web using WebRobot v1.0.

Leave a comment

Sign in or Join us (it's free).

Fernando Sanchez

Related podcasts

  • xpert to Expert: Inside Concurrent Basic (CB)

    "Concurrent Basic extends Visual Basic with stylish asynchronous concurrency constructs derived from the join calculus. Our design advances earlier MSRC work on Polyphonic C#, Comega and the Joins Library. Unlike its C# based predecessors, CB adopts a simple event-like syntax familiar to VB progr...

We'd love to hear what you think! Submit ideas or give us feedback