【问题标题】:How to upload a file and insert a statement into the database at the same time如何在上传文件的同时向数据库中插入语句
【发布时间】:2014-08-14 16:20:04
【问题描述】:

我在上传文件和同时将数据插入数据库时​​遇到问题。 它是用经典的 ASP 编写的

 Set Upload = Server.CreateObject("Persits.Upload")
 Upload.OverwriteFiles = False
 Upload.Save Server.MapPath("files/drawings/")

 sql = "Insert into Name(first,last) values ("'" & Request.form("firstname") & "','" & Request.form("lastname") & "' " )
 ADOConn.Execute sql

【问题讨论】:

  • 你确定,Server.CreateObject("Persits.Upload") 中没有错字吗?也没有名为 O'Brian 或类似名称的用户?
  • 如果我应该注释掉上传语句,则插入语句有效,如果我应该注释掉插入语句,则上传有效。但是两者结合起来就不行了!
  • @VMai Persists.Upload ProgId 是正确的,它指的是Persits Software Upload COM Component known as ASPUpload。

标签: mysql file-upload vbscript asp-classic adodb


【解决方案1】:

原因是当您执行上传时,您将请求处理为二进制,这会使Request.Form 集合无效(就像您调用Request.BinaryRead() 一样。为了解决这个问题,ASPUpload 组件提供了它自己的Form Collection其中包含已发布的表单值。

将值 Request.Form 更改为 Upload.Form 应该可以解决这种情况下的问题。

Set Upload = Server.CreateObject("Persits.Upload")
Upload.OverwriteFiles = False
Upload.Save Server.MapPath("files/drawings/")

'Also make sure the string concatenation is correct (had extra " in after "values")
sql = "Insert into Name(first,last) values ('" & Upload.Form("firstname") & "','" & Upload.Form("lastname") & "' " )
ADOConn.Execute sql

警告 上面的代码 sn-p 没有被清理并且对SQL Injection 开放,请考虑在执行前使用ADODB.Command 对象来参数化查询(关于数据类型的一些假设和尺寸已确定)。

快速示例

 Dim cmd, conn, sql

 conn = "your connection string"
 'Parametrised query
 sql = "Insert into Name(first,last) values (?, ?)"

 Set cmd = Server.CreateObject("ADODB.Command")
 With cmd
   'No need to pass ADODB.Connection pass connection string and
   'the ADODB.Command will do it for you.
   .ActiveConnection = conn
   .CommandType = adCmdText
   .CommandText = sql
   Call .Parameters.Append(.CreateParameter("first", adVarWChar, adParamInput, 100))
   Call .Parameters.Append(.CreateParameter("last", adVarWChar, adParamInput, 100))
   'Not returning any records just inserting
   Call .Execute(adExecuteNoRecords)
 End With
 'Clean-up
 Set cmd = Nothing

附带说明:

这个信息不难发现,ASPUpload Object Reference 和Online User Manual 都很好。事实上,如果您查看了Section 2.2 - FILES and FORM Collections,那么您无需在此处发帖就可以获得问题的答案。

个人经验告诉我,在处理 9/10 次第 3 方 COM 组件时,支持文档非常好,尤其是在 Persits Software 组件的情况下。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-29
    • 1970-01-01
    相关资源
    最近更新 更多