如何使用Classic ASP在SQL Server数据库中存储图像。

时间:2012-09-14 07:19:02

标签: sql asp-classic sql-server-2000 record recordset

我正在上传图片文件,因为我在表单中使用了<input type="file" name="browse_file" accept="image/*"/>

我有一个名为covers的表,其中有一个名为img的列,其数据类型为image。 这就是我的html看起来像

HTML

<table border="1" cellpadding="5" cellspacing="0" width="100%">
                <tr>
                    <td width="20%"><b>Choose Cover:</b></td>
                    <td>
                        <input type="file" name="browse_file"  accept="image/*"/>
                        <%
                            session("file_name") = request.Form("browse_file")
                        %>
                    </td>
                </tr>
                <tr>
                    <td><strong>Cover Name:</strong></td>
                    <td><input type="text" name="cover_name" value=""></td>
                </tr>
                <tr>
                    <td><strong>Choose Category:</strong></td>
                    <td>
                        <select name="disp_cat" onchange="return refresh_cat();">
                            <option selected="selected" value="0">Choose Category...</option>                           
                            <%
                                sql = "SELECT * from categories"
                                rs.open sql, con, 1, 2
                                do while not rs.eof
                            %>
                            <% if rs("name") = request.Form("disp_cat") then%>
                            <option value="<%=rs("name")%>" selected="selected"><%=rs("name")%></option>
                            <%else%>
                            <option value="<%=rs("name")%>"><%=rs("name")%></option>
                            <%end if
                                rs.movenext
                                loop
                                rs.close
                            %>
                        </select>
                    </td>
                </tr>
                <tr>    
                    <td colspan="2">
                        <input type="submit" value="Save" name="save_cover" onclick="return show_alert2();"/>
                        <input type="submit" value="Cancel" name="cancel" onclick="return go_back();"/>
                    </td>
                </tr>
                <%
                    if request.Form("save_cover") <> "" then
                    sql = "SELECT * from covers"
                    rs.open sql, con, 1, 2
                    rs.addnew

                    rs("c_name") = request.Form("cover_name")
                    rs("category") = request.Form("disp_cat")
                    rs("img") = request.Form("browse_file")
                    rs("date_upl") = date()

                    rs.update

                    response.Write("<script language='javascript'>{update1();}</script>")

                    rs.close
                    end if
                %>
                </table>

与数据库的连接在名为database.asp的单独文件中定义。连接正常,因为c_namecategorydate_upl等所有列都在数据库表covers中更新,但img列除外。

它没有占用图像路径。 rs("img") = request.Form("browse_file")是否有更新数据库列的错误的asp记录集?

1 个答案:

答案 0 :(得分:3)

如果您正在使用SQL Image Datataype,那么binary field来存储图像本身,而不是路径 - 如果您只想存储路径,则使用{{1}数据类型。

如果要将图像存储在数据库中(作为二进制文件),那么最简单的方法是将文件保存到磁盘(使用FreeASPUpload等),然后告诉SQL将该文件加载到二进制列,例如:

varchar

或者,如果要插入表单数据,请在之后将映像上传到SQL:

INSERT INTO covers(img)  
    SELECT * FROM   
    OPENROWSET(BULK N'c:\uplaods\img0001.jpg', SINGLE_BLOB) AS import  

就个人而言 - 我不会在SQL中存储图像,我将它们保存在磁盘上并将文件名存储在SQL中,例如:

UPDATE covers SET [img] = (SELECT NewImage.* from Openrowset(Bulk 'c:\uplaods\img0001.jpg', Single_Blob) NewImage) where Id = 10

编辑1:回复您的评论

我通常不会为他们编写人员代码,但你似乎在努力掌握这些基础知识,不,你不会编辑你的.inc文件。您要编辑的代码位是检查帖子并插入数据的代码,例如:

function SaveFiles
    Dim Upload, fileName, fileSize, ks, i, fileKey

    Set Upload = New FreeASPUpload
    Upload.Save(uploadsDirVar)
    If Err.Number <> 0 then Exit function
    SaveFiles = ""
    ks = Upload.UploadedFiles.keys
    if (UBound(ks) <> -1) then
        SaveFiles = "<B>The following was uploaded:</B> "
        for each fileKey in Upload.UploadedFiles.keys
            SaveFiles = SaveFiles & Upload.UploadedFiles(fileKey).FileName & " (" & Upload.UploadedFiles(fileKey).Length & "B) "

            Dim cover_name, disp_cat
            cover_name = Upload.Form("cover_name")
            disp_cat = Upload.Form("disp_cat")

            '#### Double Up any quotes (very lazy - use paramaterised SQL instead)
            cover_name = replace(cover_name, "'", "''")
            disp_cat = replace(disp_cat, "'", "''")

            'Dump file information into database
            SQLnewFile = "INSERT into covers (File_Filename, cover_name, disp_cat) " & _
                         "VALUES ('" & replace(Upload.UploadedFiles(fileKey).FileName,"'","''") & "', '" & cover_name & "', '" & disp_cat & "')"              
            db.Execute(SQLnewFile)
        next
    else
        SaveFiles = "The file name specified in the upload form does not correspond to a valid file in the system."
    end if
end function