Urgent Deliverable

Discussion in 'ASP General' started by kalikoi@gmail.com, Jan 2, 2006.

  1. Guest

    Hi All

    when i execute the following code i'm getting the error

    ****************************************************************************
    Content-type: text/html
    Software error:
    Malformed multipart POST
    *****************************************************************************


    <%
    dim strDownloadedCode1
    Class MultiPartFormPost
    Public Boundary
    Private NewData
    Private PreviousData
    Private ItemString
    Private blnLastItem
    dim http
    'The next two functions build up a string of form elements and files to
    add
    'to the form post.
    Public Sub AddFile(LocalPath,FieldName)
    ItemString = ItemString & "FILE^^" & LocalPath & "^^" & FieldName &
    "||"
    End Sub
    Public Sub AddField(FieldName,FieldValue)
    ItemString = ItemString & "FIELD^^" & FieldName & "^^" & FieldValue &
    "||"
    End Sub
    Public Function Send(URL)
    If Boundary = "" then Boundary =
    "http://SERVER2(DESTINATION)/XXXimport/code/upload.cgi-MultipartFormPost"
    'Default Boundary
    'Remove the last divider element
    ItemString = left(itemstring,len(itemstring) - 2)
    'Create an array of the various form elements to post
    Items = Split(ItemString,"||")
    For count = 0 to ubound(Items)
    'Preserve the Current Binary Data
    PreviousData = NewData
    'Grab the data needed to post each form element
    ItemPart = Split(Items(Count),"^^")
    'Are we dealing with the last element?
    If count = UBound(Items) Then blnLastItem = True
    If ItemPart(0) = "FILE" Then
    AddItem 0,ItemPart(1),ItemPart(2) 'Add File
    else
    AddItem 1,ItemPart(1),ItemPart(2) 'Add Field
    end if
    Next
    'Create HTTP object to Post the data
    Set http = CreateObject("MSXML2.ServerXMLHTTP")
    http.Open "POST", URL, False

    http.setRequestHeader "Content-Type", "multipart/form-data; boundary="
    + Boundary
    'Send the Data, and grab the response.

    http.send NewData
    If http.readyState <> 4 then
    http.waitForResponse 10
    End If
    strDownloadedCode1 = http.responseText
    set http = Nothing

    End Function
    Private Sub AddItem(FType,arg1, arg2)
    If FType = 1 then 'Add field
    NewData = BuildFormData(arg1, arg2,"")
    else 'Add file
    Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
    'Does the file exist?
    if Not objFSO.FileExists(arg1) then Exit Sub
    'Grab the file data as binary.
    Set Stream = CreateObject("ADODB.Stream")
    Stream.Type = 1 : Stream.Open : Stream.LoadFromFile arg1
    FileContents = stream.read
    'Build this elements RAW HTTP Data
    NewData = BuildFormData(FileContents, arg1, arg2)
    'Clear up!
    stream.close : set stream = nothing : Set FSO = Nothing
    end if
    End Sub
    Private Function BuildFormData(arg1, arg2, arg3)
    'Have any items been added yet?
    If lenb(PreviousData) > 0 then
    pre = vbCrLf
    else
    pre = ""
    end if
    'Arg3 will be blank if dealing with a Field Element
    If arg3 <> "" then 'File Element
    'Set the Element's preceding HTTP String
    Pre = Pre & Boundary & vbCrLf & "Content-Disposition: form-data; " & _
    "name=""" & arg3 & """; filename=""" & arg2 & """" & vbCrLf & _
    "Content-Type: application/upload" & vbCrLf & vbCrLf
    else 'Field Element
    'Set the Element's preceding HTTP String
    Pre = Pre & Boundary & vbCrLf & "Content-Disposition: form-data; " & _
    "name=""" & arg1 & """" & vbcrlf & vbcrlf
    end if
    'Are we dealing with the last element?
    If blnLastItem then
    'Set the last element's finishing HTTP String
    Po = vbcrlf & Boundary + "--" + vbCrLf
    else
    Po = ""
    end if
    'Create a recordset instance so we can manipulate binary data
    Set RS = CreateObject("ADODB.Recordset")
    If arg3 <> "" then 'File Element
    RS.Fields.Append "b", 205, Len(Pre) + LenB(arg1) + Len(Po)
    else 'Field Element
    RS.Fields.Append "b", 205, Len(Pre) + Len(arg2) + Len(Po)
    end if
    RS.Open : RS.AddNew 'Create a record within the recordset object
    LenData = Len(Pre)
    'Convert the preceeding HTTP String to binary
    RS("b").AppendChunk (StringToMB(Pre) & ChrB(0))
    Pre = RS("b").GetChunk(LenData) : RS("b") = ""
    If blnLastItem then ' Last element?
    'Convert the last element's finishing HTTP String to binary
    LenData = Len(Po) : RS("b").AppendChunk (StringToMB(Po) & ChrB(0))
    Po = RS("b").GetChunk(LenData) : RS("b") = ""
    end if
    if arg3 = "" then 'Convert Field's Value to binary.
    LenData = Len(arg2) : RS("b").AppendChunk (StringToMB(arg2) & ChrB(0))
    arg2 = RS("b").GetChunk(LenData) : RS("b") = ""
    end if
    'If there was already Binary Data (form elements), then we add this in
    front
    'of this element's binary data
    If LenB(PreviousData) > 0 then RS("b").AppendChunk (PreviousData)
    'Add the preceding HTTP String Binary
    RS("b").AppendChunk (Pre)
    if arg3 <> "" then 'Add the Binary File Data
    RS("b").AppendChunk (Arg1)
    else 'Add the Binary Field Value
    RS("b").AppendChunk (arg2)
    end if
    'If we are on the last element, add the finishing binary data
    If blnLastItem then RS("b").AppendChunk (Po)
    'Return the Binary to calling function
    RS.Update : FormData = RS("b") : BuildFormData = FormData
    'Clear up.
    RS.Close : Set RS = Nothing
    End Function
    Private Function StringToMB(S)
    'This function converts a string, to a binary string
    B = ""
    For I = 1 To Len(S)
    B = B & ChrB(Asc(Mid(S, I, 1)))
    Next
    StringToMB = B
    End Function
    End Class


    Set frmPost = New MultiPartFormPost
    'Add a file (as if you clicked the browse button on a form)
    frmPost.AddFile "C:\Inetpub\wwwroot\Satya\Elroiproc.txt","proof_file"
    'Add a Form Field
    frmPost.AddField "number","54169-15577.1"
    frmPost.AddField "id","451"
    frmPost.AddField "SUBMIT","SUBMIT"
    'you can call the above steps as many times as nessesary
    'next, we send the output to the form
    frmPost.Send("http://SERVER2(DESTINATION)/XXXimport/code/upload.cgi")

    Response.Write strDownloadedCode1&"<br>"
    Set frmPost = Nothing

    %>
     
    , Jan 2, 2006
    #1
    1. Advertising

  2. Mike Guest

    wrote:
    > Hi All
    >
    > when i execute the following code i'm getting the error
    >
    > ****************************************************************************
    > Content-type: text/html
    > Software error:
    > Malformed multipart POST
    > *****************************************************************************
    >
    >
    > <%
    > dim strDownloadedCode1
    > Class MultiPartFormPost
    > Public Boundary
    > Private NewData
    > Private PreviousData
    > Private ItemString
    > Private blnLastItem
    > dim http
    > 'The next two functions build up a string of form elements and files to
    > add
    > 'to the form post.
    > Public Sub AddFile(LocalPath,FieldName)
    > ItemString = ItemString & "FILE^^" & LocalPath & "^^" & FieldName &
    > "||"
    > End Sub
    > Public Sub AddField(FieldName,FieldValue)
    > ItemString = ItemString & "FIELD^^" & FieldName & "^^" & FieldValue &
    > "||"
    > End Sub
    > Public Function Send(URL)
    > If Boundary = "" then Boundary =
    > "http://SERVER2(DESTINATION)/XXXimport/code/upload.cgi-MultipartFormPost"
    > 'Default Boundary
    > 'Remove the last divider element
    > ItemString = left(itemstring,len(itemstring) - 2)
    > 'Create an array of the various form elements to post
    > Items = Split(ItemString,"||")
    > For count = 0 to ubound(Items)
    > 'Preserve the Current Binary Data
    > PreviousData = NewData
    > 'Grab the data needed to post each form element
    > ItemPart = Split(Items(Count),"^^")
    > 'Are we dealing with the last element?
    > If count = UBound(Items) Then blnLastItem = True
    > If ItemPart(0) = "FILE" Then
    > AddItem 0,ItemPart(1),ItemPart(2) 'Add File
    > else
    > AddItem 1,ItemPart(1),ItemPart(2) 'Add Field
    > end if
    > Next
    > 'Create HTTP object to Post the data
    > Set http = CreateObject("MSXML2.ServerXMLHTTP")
    > http.Open "POST", URL, False
    >
    > http.setRequestHeader "Content-Type", "multipart/form-data; boundary="
    > + Boundary
    > 'Send the Data, and grab the response.
    >
    > http.send NewData
    > If http.readyState <> 4 then
    > http.waitForResponse 10
    > End If
    > strDownloadedCode1 = http.responseText
    > set http = Nothing
    >
    > End Function
    > Private Sub AddItem(FType,arg1, arg2)
    > If FType = 1 then 'Add field
    > NewData = BuildFormData(arg1, arg2,"")
    > else 'Add file
    > Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
    > 'Does the file exist?
    > if Not objFSO.FileExists(arg1) then Exit Sub
    > 'Grab the file data as binary.
    > Set Stream = CreateObject("ADODB.Stream")
    > Stream.Type = 1 : Stream.Open : Stream.LoadFromFile arg1
    > FileContents = stream.read
    > 'Build this elements RAW HTTP Data
    > NewData = BuildFormData(FileContents, arg1, arg2)
    > 'Clear up!
    > stream.close : set stream = nothing : Set FSO = Nothing
    > end if
    > End Sub
    > Private Function BuildFormData(arg1, arg2, arg3)
    > 'Have any items been added yet?
    > If lenb(PreviousData) > 0 then
    > pre = vbCrLf
    > else
    > pre = ""
    > end if
    > 'Arg3 will be blank if dealing with a Field Element
    > If arg3 <> "" then 'File Element
    > 'Set the Element's preceding HTTP String
    > Pre = Pre & Boundary & vbCrLf & "Content-Disposition: form-data; " & _
    > "name=""" & arg3 & """; filename=""" & arg2 & """" & vbCrLf & _
    > "Content-Type: application/upload" & vbCrLf & vbCrLf
    > else 'Field Element
    > 'Set the Element's preceding HTTP String
    > Pre = Pre & Boundary & vbCrLf & "Content-Disposition: form-data; " & _
    > "name=""" & arg1 & """" & vbcrlf & vbcrlf
    > end if
    > 'Are we dealing with the last element?
    > If blnLastItem then
    > 'Set the last element's finishing HTTP String
    > Po = vbcrlf & Boundary + "--" + vbCrLf
    > else
    > Po = ""
    > end if
    > 'Create a recordset instance so we can manipulate binary data
    > Set RS = CreateObject("ADODB.Recordset")
    > If arg3 <> "" then 'File Element
    > RS.Fields.Append "b", 205, Len(Pre) + LenB(arg1) + Len(Po)
    > else 'Field Element
    > RS.Fields.Append "b", 205, Len(Pre) + Len(arg2) + Len(Po)
    > end if
    > RS.Open : RS.AddNew 'Create a record within the recordset object
    > LenData = Len(Pre)
    > 'Convert the preceeding HTTP String to binary
    > RS("b").AppendChunk (StringToMB(Pre) & ChrB(0))
    > Pre = RS("b").GetChunk(LenData) : RS("b") = ""
    > If blnLastItem then ' Last element?
    > 'Convert the last element's finishing HTTP String to binary
    > LenData = Len(Po) : RS("b").AppendChunk (StringToMB(Po) & ChrB(0))
    > Po = RS("b").GetChunk(LenData) : RS("b") = ""
    > end if
    > if arg3 = "" then 'Convert Field's Value to binary.
    > LenData = Len(arg2) : RS("b").AppendChunk (StringToMB(arg2) & ChrB(0))
    > arg2 = RS("b").GetChunk(LenData) : RS("b") = ""
    > end if
    > 'If there was already Binary Data (form elements), then we add this in
    > front
    > 'of this element's binary data
    > If LenB(PreviousData) > 0 then RS("b").AppendChunk (PreviousData)
    > 'Add the preceding HTTP String Binary
    > RS("b").AppendChunk (Pre)
    > if arg3 <> "" then 'Add the Binary File Data
    > RS("b").AppendChunk (Arg1)
    > else 'Add the Binary Field Value
    > RS("b").AppendChunk (arg2)
    > end if
    > 'If we are on the last element, add the finishing binary data
    > If blnLastItem then RS("b").AppendChunk (Po)
    > 'Return the Binary to calling function
    > RS.Update : FormData = RS("b") : BuildFormData = FormData
    > 'Clear up.
    > RS.Close : Set RS = Nothing
    > End Function
    > Private Function StringToMB(S)
    > 'This function converts a string, to a binary string
    > B = ""
    > For I = 1 To Len(S)
    > B = B & ChrB(Asc(Mid(S, I, 1)))
    > Next
    > StringToMB = B
    > End Function
    > End Class
    >
    >
    > Set frmPost = New MultiPartFormPost
    > 'Add a file (as if you clicked the browse button on a form)
    > frmPost.AddFile "C:\Inetpub\wwwroot\Satya\Elroiproc.txt","proof_file"
    > 'Add a Form Field
    > frmPost.AddField "number","54169-15577.1"
    > frmPost.AddField "id","451"
    > frmPost.AddField "SUBMIT","SUBMIT"
    > 'you can call the above steps as many times as nessesary
    > 'next, we send the output to the form
    > frmPost.Send("http://SERVER2(DESTINATION)/XXXimport/code/upload.cgi")
    >
    > Response.Write strDownloadedCode1&"<br>"
    > Set frmPost = Nothing
    >
    > %>


    This is a classic ASP news group. Your question relates to ASP.NET.
    You are much more likely to find someone to help you in one of the .NET
    groups. I suggest microsoft.public.dotnet.framework.aspnet

    Mike
     
    Mike, Jan 2, 2006
    #2
    1. Advertising

  3. Guest

    Hi Mike

    This code is in asp only.Not dotnet.
     
    , Jan 2, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sachin Jagtap

    URGENT !! QUEUE STL PROBLEM URGENT!!

    Sachin Jagtap, Apr 28, 2005, in forum: C++
    Replies:
    4
    Views:
    1,055
    Mark Stijnman
    Apr 29, 2005
  2. Urgent deliverable

    , Aug 28, 2006, in forum: ASP General
    Replies:
    7
    Views:
    149
    Bob Barrows [MVP]
    Aug 29, 2006
  3. Rob
    Replies:
    3
    Views:
    450
  4. kalyan

    urgent deliverable

    kalyan, Oct 10, 2006, in forum: Javascript
    Replies:
    1
    Views:
    137
    Jeremy
    Oct 11, 2006
  5. Replies:
    1
    Views:
    278
Loading...

Share This Page