使用ADODB.Stream保存POST二进制数据

时间:2013-03-17 18:02:29

标签: asp-classic ado

另一个网站使用POST方法向我发送数据。

我想获取这些数据并将其插入数据库。

经过网上的一些研究,我得出结论,ADODB.Stream应该为我做好工作。

我没有使用Request.TotalBytes获取二进制数据的问题。 使用以下代码,我没有收到错误,但它也没有保存数据。所以我必须对ADODB Stream做错了。

    tot_bytes = Request.TotalBytes

    Set BinaryStream = CreateObject("ADODB.Stream")
    BinaryStream.Mode = 3
    BinaryStream.Type = 1
    BinaryStream.Open       
    gBinaryData = BinaryStream.Write(tot_bytes) 
    BinaryStream.Close
    Set BinaryStream = Nothing

    SQL = "INSERT INTO STATUSES (StatusMessage, StatusDateEntered) VALUES ('"& gBinaryData &"', '"& FormatDateMySQL(NOW) &"')"                                  
    Set objAddC = objConn.execute(SQL)

Following a successful subscription, Facebook will proceed to call your endpoint every time that there are changes (to the chosen fields or connections). For each update, it will make an HTTP POST request.

The request will have content type of application/json and the body will comprise a JSON-encoded string containing one or more changes.

Note for PHP developers: In PHP, to get the encoded data you would use the following code:

$data = file_get_contents("php://input");
$json = json_decode($data);

2 个答案:

答案 0 :(得分:5)

首先,Write方法不会返回任何内容,实际上它只是一个子例程。而Request.TotalBytes只是请求的长度,以字节为单位。当您需要将请求数据作为字节数组读取时,应使用Request.BinaryRead(字节长度)。因此,在Stream对象中,您需要在写入字节并将位置设置为开始后使用Read方法读取所有字节。

但是,如果您必须将数据存储为二进制文件,则在这种情况下似乎不是必需的 我假设你需要数据作为文本,很可能是json字符串。因此,您应该将数据从字节转换为字符串。

请注意,您需要处理有关总字节数的异常。如果请求不包含任何内容,Request.TotalBytes等于零,并且Request.BinaryRead期望大于零且小于或等于总字节的数字发生错误。

Dim tot_bytes, postData
    tot_bytes = Request.TotalBytes
If tot_bytes > 0 Then
    With Server.CreateObject("Adodb.Stream")
        .Charset = "utf-8" 'specify the request encoding
        .Type = 1 'adTypeBinary, a binary stream
        .Open
        .Write Request.BinaryRead(tot_bytes) 'Write bytes
        .Position = 0 ' set position to the start
        .Type = 2 ' adTypeText, stream type is text now
        postData = .ReadText 'read all text
        .Close
    End With
    With Server.CreateObject("Adodb.Recordset")
        .Open "STATUSES", objConn , , 3
        .AddNew
        .Fields("StatusMessage").Value = postData
        .Fields("StatusDateEntered").Value = Now()
        .Update
        .Close
    End With
    Response.Write "data stored successfully"
Else
    Response.Write "no post data"
End If

答案 1 :(得分:0)

除了直接将数据插入这样的数据库(可能是sql注入)这是一个非常糟糕的事实,你如何发布表单数据? CLassic ASP也无法直接处理二进制数据。所以这根本不起作用。

因此,无论您发布什么内容,首先您必须确保使用表单enctype="multiform/data"发帖。 要将数据导入对象,请尝试以下方法:

byteArray = Request.BinaryRead(Request.TotalBytes)

要处理它,存储到数据库或保存到文件,你需要一个组件,例如: http://www.motobit.com/help/scptutl/upload.asp或尝试查看此文章(特别是当您上传更多文件时只有一个文件):http://www.codeguru.com/csharp/.net/net_asp/article.php/c19297/Pure-ASP-File-Upload.htm

编辑: Antonin Fuller还在不使用他的DLL的情况下制作了一个示例ASP代码。 试试这个:

Private Function RSBinaryToString(xBinary)
'Antonin Foller, http://www.motobit.com
'RSBinaryToString converts binary data (VT_UI1 | VT_ARRAY Or MultiByte string)
'to a string (BSTR) using ADO recordset

Dim Binary
'MultiByte data must be converted To VT_UI1 | VT_ARRAY first.
If vartype(xBinary)=8 Then Binary = MultiByteToBinary(xBinary) Else Binary = xBinary

Dim RS, LBinary
Const adLongVarChar = 201
Set RS = CreateObject("ADODB.Recordset")
LBinary = LenB(Binary)

If LBinary>0 Then
    RS.Fields.Append "mBinary", adLongVarChar, LBinary
    RS.Open
    RS.AddNew
    RS("mBinary").AppendChunk Binary 
    RS.Update
    RSBinaryToString = RS("mBinary")
Else  
    RSBinaryToString = ""
End If
End Function

在此处查看更多内容:http://www.motobit.com/tips/detpg_binarytostring/

最后你应该获得流,转换它,并使用它。