C
Chris Millar
Can anyone help me on converting this vb asp page to C#,
thanks in advance.
chris.
<!DOCTYPE HTML PUBLIC "-//W3C//Dtd HTML 4.0 transitional//EN">
<%
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''
'
' Apart from the trivial task of instantiating
' an object and making the calls, this includes extracting binary data from
' the POST command (without resorting to the posting acceptor, cpshost.dll).
'
' To use this functionality, the invoking form must be set up to use the
' post method, and have encoding set to multipart/form-data, like this:
' <form method="post" action="tagged.asp" enctype="multipart/form-data">
'
' Because we have binary data coming in, we have to do all the data handling
' ourselves. Note, this doesn't mix with regular field access (e.g.
' Request.Form("myfield")); if you need both, you should use multiple forms
' with different enctype settings.
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''
'
' ASP is not very friendly about managing binary data. Basically, a string
' is allowed to contain non-UNICODE data, including embedded nulls. The 'B'
' functions (LenB, MidB, etc) know this and can be used to manipulate such
' string. Likewise, LensTalker has methods called TagText and TagTextB.
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''
' This function converts binary data to a string
Function BinToStr(Data)
BinToStr = ""
For n = 1 to LenB(Data)
BinToStr = BinToStr & Chr(AscB(MidB(Data,n,1)))
Next
End Function
' This function converts a string to binary data
Function StrToBin(Str)
StrToBin = ""
For n = 1 to Len(Str)
StrToBin = StrToBin & ChrB(AscB(Mid(Str,n,1)))
Next
End Function
' This function retrieves a field's name from an Info block
' The info block looks something like this:
' Content-Disposition: form-data; name="localfile";
' filename="D:\projects\dev\bin\bug\aaronj1.cln"
' Content-Type: text/plain
' We just scan for the fieldname followed by "=" and extract value.
Function GetInfoField(Info, Field)
GetInfoField = ""
prompt = Field & "=" & Chr(34) ' that's a double-quote
offset = Len(prompt)
spos = InStr(Info, prompt)
If spos > 0 Then
epos = InStr(spos + offset, Info, Chr(34)) ' that's a double-quote
If epos > 0 Then
GetInfoField = Mid(Info, spos + offset, epos - spos - offset)
End If
End If
End Function
' okay, get the data and parse it
Dim biData
biData = Request.BinaryRead(Request.TotalBytes)
Dim plaintext, filedata, tagtext
' The encoding has to be what we expect (multipart/form-data)
' Content type looks something like this:
' multipart/form-data;
boundary=---------------------------7d235e3a10d07ba
ContentType = Request.ServerVariables("HTTP_CONTENT_TYPE")
ctArray = Split(ContentType, ";")
If Trim(ctArray(0)) = "multipart/form-data" Then
' When the boundary is actually inserted into the data, two preceeding
' dashes are included ... for ease, we'll just add them now ...
Boundary = StrToBin("--" & Trim(Split(Trim(ctArray(1)), "=")(1)))
' some useful constants
InfoDelim = StrToBin(vbCrLf & vbCrLf)
LenData = LenB(biData)
LenBoundary = LenB(Boundary)
LenInfoDelim = LenB(InfoDelim)
Dim FieldStart, FieldEnd, InfoEnd
Dim Info, Value, Name
' Parse thru all the fields. Each field consists of:
' boundary & CrLf
' information about the field & CrLf & CrLf
' value of the field
' (there is a final boundary followed by "--")
FieldStart = 1
While FieldStart > 0 And FieldStart < LenData
' skip over the boundary (and CrLf) and find end of field
FieldStart = FieldStart + LenBoundary + 2
FieldEnd = InStrB(FieldStart, biData, Boundary)
' extract the field info
InfoEnd = InStrB(FieldStart, biData, InfoDelim)
If InfoEnd > 0 Then
Info = MidB(biData, FieldStart, InfoEnd - FieldStart)
InfoEnd = InfoEnd + LenInfoDelim
Value = MidB(biData, InfoEnd, FieldEnd - InfoEnd - 2)
Info = BinToStr(Info)
Name = GetInfoField(Info, "name")
' we are expecting two fields, localfile and plaintext
' (your field handling will, obviously, vary)
If Name = "localfile" Then
filedata = Value
ElseIf Name = "plaintext" Then
plaintext = Trim(BinToStr(Value))
End If
End If
' move to next field and check for final boundary
If BinToStr(MidB(biData,FieldEnd+LenBoundary,2)) = "--" Then
FieldStart = LenData
Else
FieldStart = FieldEnd
End If
Wend
End If
' Now the call to Lens is pretty easy ...
Set talker = CreateObject("bgtcom.LensTalker")
talker.port = 2000
talker.address = "localhost"
If LenB(filedata) > 0 Then
tagtext = talker.TagTextB(filedata)
ElseIf plaintext <> "" Then
tagtext = talker.TagText(plaintext)
Else
tagtext = "<error>Error: nothing to do!</error>"
End If
Set talker = Nothing
' Parse and/or transform the XML
Dim style
Set style = CreateObject("microsoft.xmldom")
style.async = False
If style.load("f:/inetpub/wwwroot/xsl/brassring.xsl") = False Then
style.loadXML("<error>Error: couldn't load style</error>")
End If
Dim doc
Set doc = CreateObject("microsoft.xmldom")
doc.async = False
If doc.loadXML(tagtext) = False Then
doc.loadXML("<error>Error: couldn't load tagged text</error>")
End If
%>
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=unicode">
</head>
<body>
<%=doc.transformNode(style)%>
<form name="debug">
<table cellSpacing=1 cellPadding=1 width="80%" align=center border=0>
<tr>
<td align="left">
<textarea rows="10" cols="90"><%=doc.text%></textarea>
</td>
</tr>
<tr>
<td align="left">
<textarea rows="10" cols="90"><%=doc.xml%></textarea>
</td>
</tr>
</table>
</form>
</body>
</html>
<%
Set doc = Nothing
Set style = Nothing
%>
here is what i have so far.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
namespace BurningGlass
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnSubmit;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnSubmit_Click(object sender, System.EventArgs e)
{
byte[] biData;
// define which character is seperating fields
char[] delimChar = {';'};
biData = Request.BinaryRead(Request.TotalBytes);
string ContentType = Request.ServerVariables["HTTP_CONTENT_TYPE"];
string[] ctArray = ContentType.Split(delimChar);
if (ctArray[0].Trim() == "multipart/form-data")
{
// When the boundary is actually inserted into the data, two preceeding
// dashes are included ... for ease, we'll just add them now ...
//Boundary = StrToBin("--" & Trim(Split(Trim(ctArray(1)), "=")(1)))
StringBuilder strBoundary = new StringBuilder();
strBoundary.Append("--");
strBoundary.Append(ctArray[1].Split('='));
byte Boundary = Convert.to(strBoundary);
//string strBoundary = "--" & ctArray[1].Trim().Split('=');
//string[] boundary = ctArray[1].Split('=');
}
}
// private string BinToStr(byte[] Data)
// {
// string BinToStr = "";
// for (int n = 1 ; Data.Length; n++)
// {
// string BinToStr = BinToStr & Chr(AscB(MidB(Data,n,1)));
// }
//
// }
//
// ' This function converts a string to binary data
// Function StrToBin(Str)
// StrToBin = ""
// For n = 1 to Len(Str)
// StrToBin = StrToBin & ChrB(AscB(Mid(Str,n,1)))
// Next
// End Function
//
}
}
thanks in advance.
chris.
<!DOCTYPE HTML PUBLIC "-//W3C//Dtd HTML 4.0 transitional//EN">
<%
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''
'
' Apart from the trivial task of instantiating
' an object and making the calls, this includes extracting binary data from
' the POST command (without resorting to the posting acceptor, cpshost.dll).
'
' To use this functionality, the invoking form must be set up to use the
' post method, and have encoding set to multipart/form-data, like this:
' <form method="post" action="tagged.asp" enctype="multipart/form-data">
'
' Because we have binary data coming in, we have to do all the data handling
' ourselves. Note, this doesn't mix with regular field access (e.g.
' Request.Form("myfield")); if you need both, you should use multiple forms
' with different enctype settings.
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''
'
' ASP is not very friendly about managing binary data. Basically, a string
' is allowed to contain non-UNICODE data, including embedded nulls. The 'B'
' functions (LenB, MidB, etc) know this and can be used to manipulate such
' string. Likewise, LensTalker has methods called TagText and TagTextB.
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''
' This function converts binary data to a string
Function BinToStr(Data)
BinToStr = ""
For n = 1 to LenB(Data)
BinToStr = BinToStr & Chr(AscB(MidB(Data,n,1)))
Next
End Function
' This function converts a string to binary data
Function StrToBin(Str)
StrToBin = ""
For n = 1 to Len(Str)
StrToBin = StrToBin & ChrB(AscB(Mid(Str,n,1)))
Next
End Function
' This function retrieves a field's name from an Info block
' The info block looks something like this:
' Content-Disposition: form-data; name="localfile";
' filename="D:\projects\dev\bin\bug\aaronj1.cln"
' Content-Type: text/plain
' We just scan for the fieldname followed by "=" and extract value.
Function GetInfoField(Info, Field)
GetInfoField = ""
prompt = Field & "=" & Chr(34) ' that's a double-quote
offset = Len(prompt)
spos = InStr(Info, prompt)
If spos > 0 Then
epos = InStr(spos + offset, Info, Chr(34)) ' that's a double-quote
If epos > 0 Then
GetInfoField = Mid(Info, spos + offset, epos - spos - offset)
End If
End If
End Function
' okay, get the data and parse it
Dim biData
biData = Request.BinaryRead(Request.TotalBytes)
Dim plaintext, filedata, tagtext
' The encoding has to be what we expect (multipart/form-data)
' Content type looks something like this:
' multipart/form-data;
boundary=---------------------------7d235e3a10d07ba
ContentType = Request.ServerVariables("HTTP_CONTENT_TYPE")
ctArray = Split(ContentType, ";")
If Trim(ctArray(0)) = "multipart/form-data" Then
' When the boundary is actually inserted into the data, two preceeding
' dashes are included ... for ease, we'll just add them now ...
Boundary = StrToBin("--" & Trim(Split(Trim(ctArray(1)), "=")(1)))
' some useful constants
InfoDelim = StrToBin(vbCrLf & vbCrLf)
LenData = LenB(biData)
LenBoundary = LenB(Boundary)
LenInfoDelim = LenB(InfoDelim)
Dim FieldStart, FieldEnd, InfoEnd
Dim Info, Value, Name
' Parse thru all the fields. Each field consists of:
' boundary & CrLf
' information about the field & CrLf & CrLf
' value of the field
' (there is a final boundary followed by "--")
FieldStart = 1
While FieldStart > 0 And FieldStart < LenData
' skip over the boundary (and CrLf) and find end of field
FieldStart = FieldStart + LenBoundary + 2
FieldEnd = InStrB(FieldStart, biData, Boundary)
' extract the field info
InfoEnd = InStrB(FieldStart, biData, InfoDelim)
If InfoEnd > 0 Then
Info = MidB(biData, FieldStart, InfoEnd - FieldStart)
InfoEnd = InfoEnd + LenInfoDelim
Value = MidB(biData, InfoEnd, FieldEnd - InfoEnd - 2)
Info = BinToStr(Info)
Name = GetInfoField(Info, "name")
' we are expecting two fields, localfile and plaintext
' (your field handling will, obviously, vary)
If Name = "localfile" Then
filedata = Value
ElseIf Name = "plaintext" Then
plaintext = Trim(BinToStr(Value))
End If
End If
' move to next field and check for final boundary
If BinToStr(MidB(biData,FieldEnd+LenBoundary,2)) = "--" Then
FieldStart = LenData
Else
FieldStart = FieldEnd
End If
Wend
End If
' Now the call to Lens is pretty easy ...
Set talker = CreateObject("bgtcom.LensTalker")
talker.port = 2000
talker.address = "localhost"
If LenB(filedata) > 0 Then
tagtext = talker.TagTextB(filedata)
ElseIf plaintext <> "" Then
tagtext = talker.TagText(plaintext)
Else
tagtext = "<error>Error: nothing to do!</error>"
End If
Set talker = Nothing
' Parse and/or transform the XML
Dim style
Set style = CreateObject("microsoft.xmldom")
style.async = False
If style.load("f:/inetpub/wwwroot/xsl/brassring.xsl") = False Then
style.loadXML("<error>Error: couldn't load style</error>")
End If
Dim doc
Set doc = CreateObject("microsoft.xmldom")
doc.async = False
If doc.loadXML(tagtext) = False Then
doc.loadXML("<error>Error: couldn't load tagged text</error>")
End If
%>
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=unicode">
</head>
<body>
<%=doc.transformNode(style)%>
<form name="debug">
<table cellSpacing=1 cellPadding=1 width="80%" align=center border=0>
<tr>
<td align="left">
<textarea rows="10" cols="90"><%=doc.text%></textarea>
</td>
</tr>
<tr>
<td align="left">
<textarea rows="10" cols="90"><%=doc.xml%></textarea>
</td>
</tr>
</table>
</form>
</body>
</html>
<%
Set doc = Nothing
Set style = Nothing
%>
here is what i have so far.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
namespace BurningGlass
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnSubmit;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnSubmit_Click(object sender, System.EventArgs e)
{
byte[] biData;
// define which character is seperating fields
char[] delimChar = {';'};
biData = Request.BinaryRead(Request.TotalBytes);
string ContentType = Request.ServerVariables["HTTP_CONTENT_TYPE"];
string[] ctArray = ContentType.Split(delimChar);
if (ctArray[0].Trim() == "multipart/form-data")
{
// When the boundary is actually inserted into the data, two preceeding
// dashes are included ... for ease, we'll just add them now ...
//Boundary = StrToBin("--" & Trim(Split(Trim(ctArray(1)), "=")(1)))
StringBuilder strBoundary = new StringBuilder();
strBoundary.Append("--");
strBoundary.Append(ctArray[1].Split('='));
byte Boundary = Convert.to(strBoundary);
//string strBoundary = "--" & ctArray[1].Trim().Split('=');
//string[] boundary = ctArray[1].Split('=');
}
}
// private string BinToStr(byte[] Data)
// {
// string BinToStr = "";
// for (int n = 1 ; Data.Length; n++)
// {
// string BinToStr = BinToStr & Chr(AscB(MidB(Data,n,1)));
// }
//
// }
//
// ' This function converts a string to binary data
// Function StrToBin(Str)
// StrToBin = ""
// For n = 1 to Len(Str)
// StrToBin = StrToBin & ChrB(AscB(Mid(Str,n,1)))
// Next
// End Function
//
}
}