Single image to the database

zhaozj2021-02-16  44

The first weight: Single picture is not new, there is no new problem, but there are also a lot of tutorials online, but most is a fish, not to teach people to fish, have a hard information collection, thinking, debugging , Organize, I basically made this problem from principle. Now, based on my own understanding, I explain it on the basis of the example program, I hope to be able to have some netizens (more than me, :-)) Helped. Please refer to the unhealthy or bad of yourself. The use of Stream objects in the program refers to the "State HTTP Upmit Prorsion 2.0" in the code, which is a sincere thanks to Daoxiang Old farmers and Liang's fear. I want to step by step. Tell a simple one today, and a single image file is saved to the database. This example includes three ASP files and a database (a table), all in the same directory. 1, TBLIMAGE table structure (Access 2000) SN Auto Number Serial Number Content-Type Text Picture Type Image Ole Object Image Data 2, SimpleImageTodata.asp: Upload Form and Save Pictures to Database Code, Main Files.

<% @ Language = vbscript%> <% option explicit%> <% 'from a full path to default file name function getFileNameFromPath (StrPath) getFileNameFromPath = MID (StrPath, Instrrev (strpath, "/") 1) end function' Define database connection string DIM CNSTR CNSTR = "driver = {Microsoft Access Driver (* .mdb)}; DBQ =" & Server.mAppath ("./ Upload.mdb")%> Single image Save to Database </ Title> <meta http-equiv = "content-type" content = "text / html; charSet = GB2312"> </ head> <body> <p> <a href = "SimpleImageTodata.asp "> Upload picture </a <a href="showimagelistfromData.asp"> display picture <hr> </ p> <% if request.servervariables (" request_method ") =" post "The Dim Scome, SGO, Bindata, StrData Dim POSB, POSE, POSSB, POSSE DIM BINCRLF DIM STRPATH, STRFILENAME, STRCONTENTTYPE BINCRLF = Chrb (13) & chrb (10) 'Defines a single byte Enter SET SCOME = Server.createObject (" Adodb.Stream ") SCOME.TYPE = 1 'Specify return data type adtypebinary = 1, adtypetext = 2 Scome.mode = 3' Specify Open mode AdmodeRead = 1, AdmodeRite = 2, AdmodeReadwrite = 3 Scome.open Scome.write Request. BinaryRead (Request.Totalbytes) Scome.Position = 0 Bindata = SCOME.Read 'response.binaryWrite Bindata' debugging: Display Submit all data 'response.write "<hr>" debug set sog = server.createObject ("adodb.stream" sgo.type = 1 sgo.mode = 3 sgo.open POSB = 1 POSB = INSTRB (POSB, BINDATA, BINCRLF) POSE = INSTRB (POSB 1, Bindata, BinCrlf) 'Response.write Posb & "| & Pose &" <</p> <p>Br> "Scome.Position = POSB 1 scrosb-2 sgo.position = 0 sgo.type = 2 sgo.charset =" gb2312 "strdata = sgo.readtext sgo.close 'response.write strdata & "<hr>" POSSB = 1 Possb = INSTR (Possb, Strdata, "FileName =" "") LEN ("FileName =" ") Posse = Instr (Possb, Strdata," "") IF Posse> Possb the strpath = MID (strdata, possb, posse-possb) 'response.write "local path:" & strpath & "<br>"' response.write "file name:" & getFileNameFromPath (StrPath) & "<br> "POSB = POSE POSE = INSTRB (POSB 1, Bindata, BinCrlf) 'Response.write POSB &" | "& Pose &" <br> "SGO.TYPE = 1 SGO.MODE = 3 sgo.open scrut.position = POSB SCOME.COPYTO SGO, POSE-POSB-1 SGO.POSITION = 0 sgo.type = 2 sgo.charset = "GB2312" strdata = sgo.readtext sgo.close strcontentType = MID (strdata, 16) 'This is fixed because of fixed So I omitted :-) 'response.write "Picture Type:" & strContentType & "<HR>" POSB = POSE 2 POSE = INSTRB (POSB 1, Bindata, BinCrlf)' Response.write Posb & "|" & pose & "<b R> "sgo.type = 1 sgo.mode = 3 sgo.open scrut pPosition = POSB 1 scrodtata = sgo.read sgo.close 'response.write LENB (STRDATA) & "<br>" DIM CN, RS, SQL SET CN = Server.createObject ("</p> <p>AdoDb.connection ") CN.Open CNSTR SET RS = Server.createObject (" AdoDb.Recordset ") SQL =" Select * from tblimage "RS.Open SQL, CN, 1, 3 rs.addnew rs.fields (" Content- Type "). Value = strcontenttype rs.fields (" image "). Appendchunk strdata rs.Update = nothing cnclose set cn = Nothing response.write" The picture saved successfully! "&" <br> "Else Response.write" no upload picture! "&" <br> "end if set sg = nothing screen.close set screen = nothing else%> <form id =" frmupload "name =" frmupload "action =" simpleImagetodata.asp "method =" post "target =" _Self "EncType =" multipart / form-data "> <input id =" filimage "type =" file "name =" filimage "size =" 40 "> <br> <input =" btnupload "type =" submit " Value = "Upload" name = "btnupload"> </ form> <% end if%> </ body> </ html> 3, showimagelistfromData.asp <% @ Language = VBScript%> <% option explicit%> <HTML > <head> <title> List of available images in the database </ title> <meta http-equiv = "content-type" content = "text / html; charSet = GB2312> </ head> <body> < P> <a href="simpleImagetodata.asp"> Upload picture </a> <a href="showimagelistfromData.asp"> display picture </a> <hr> </ p> <table border = 0 cellpadding = 2 cellspacing = 2> <tr> <td valign = TOP> <% DIM CNSTR CNSTR = "driver =</p> <p>{Microsoft Access Driver (* .mdb)}; DBQ = "& Server.mappath (" ./ Upload.mdb ") DIM CN, SQL, RS SET CN = Server.createObject (" AdoDb.Connection ") CN.Open CNSTR SQL = "SELECT SN, [Content-Type], Image from TBLIMAGE" set = cn.execute (SQL) response.write "<table border = 1 Cellspacing = 2 Cellpadding = 5>" response.write "<tr>" Response.write "<th> Serial number </ th> <TH> Picture type </ tH> <TH> Image </ TH>" response.write "</ tr>" do untric r.eof response.write "< Tr> "Response.write" <TD> "& rs (" SN ") &" Response.write "<TD>" & RS ("Content-Type" & "</ TD>" Response .Write "<td> <a href='ashyimagelistfromData.asp?sn=" & rs ("sn") &" 'View pictures </a> </ td> "response.write" </ tr> "rs .MOVENEXT loop response.write "</ table>" cn.close set cn = nothing%> </ td> <td valign = top> <% DIM SN SN = Request.QueryString ("SN") IF Sn = "" Then Response.write "No specified image! "Else response.write <img border = 1 src = showImageFromData.asp? SN =" & SN & "> END IF%> </ td> </ tr> </ table> </ body> </ html> 4, showimagefromData.asp <% @ language = VBScript%> <% option explicit%> <% DIM SN SN = Request.QueryString ("SN") if Sn = "" THEN Response.end Dim CNSTR = "driver =</p> <p>{Microsoft Access Driver (* .mdb)}; DBQ = "& Server.mappath (" ./ Upload.mdb ") DIM CN, SQL, RS SET CN = Server.createObject (" AdoDb.Connection ") CN.Open CNSTR SQL = "SELECT SN, [Content-Type], Image from TBLIMAGE WHERE SN =" & CINT (SN) set = cn.execute (SQL) response.contentType = RS ("Content-Type") Response.binaryWrite RS ( "image") SET RS = Nothing cn.close set cn = nothing%> Renewal ... Author related articles: ASP file uploads the second heavy power of God Power (see the map plus internal work "(original) website creative design ( Favorites) Meta tags of the mystery (collection)</p> <p>Other related articles: Upload Download with JSPSmartupload (Original) Using Smartupload Components Using Smartupload Components in JSP (Original) Using Smartupload Components Upload File (Original) Implement Multi-File Upload (Original) in ASP.NET (original) Upload (original)</p> <p>Comments on this article</p> <p><script src = "/ wevelop / readnum.asp? id = 19179> </ script> Popularity: 2536</p> <p>In Anition (2003-6-26 23:10:17) is behind, it is easy to happen, if it is too deep, I am afraid I can't understand. I am watching the "Treatment HTTP Upload Procedure" (70 and 2.0), because it has been integrated into a class, it is simple, I want to know the principle, I have a difficult, I have seen a few nights ( I am not a pleasure). Especially the problem of processing Chinese, thanks to Daoxiang old farmers, thank you again, you can also download a look, it is worth a few nights.</p> <p>Second weight: Text information and image files are submitted to the database image file. This issue can be saved to disk files. This problem is no new problem. There are also a lot of tutorials online, but most is a fish, not People collect, think, debug, and organize, I basically have made this question from principle. Now, based on my own understanding, I will explain it, I hope to be Some netizens (more than me, :-)) help. Please refer to the unhealthy or bad of yourself. The use of Stream objects in the program refers to the "State HTTP Upmit Prorsion 2.0" in the code, which is a sincere thanks to Daoxiang Old farmers and Liang's fear. Last time, a single image file saved to the database. This time, the text information is saved to the database at the same time as the image file, and the image file can also be saved to disk files.</p> <p>MultiInputOrImageToData.asp <% @ Language = VBScript%> <% option explicit%> <% 'period of the binary data is written to a file sub saveBin2File (srmSource, posB, posLen, strPath) dim srmObj set srmObj = server.CreateObject ( " AdoDb.Stream ") SRMOBJ.TYPE = 1 SRMOBJ.MODE = 3 SRMOBJ.Open Srmsource.Position = POSB-1 SRMSource.copyto Srmobj, Poslen Srmobj.Position = 0 Srmobj.Savetofile StrPath, 2 'If the file already exists, unconditional covering srmObj.Close set srmObj = nothing end sub 'into a string of binary data, including characters function getTextfromBin (srmSource, posBegin, posLen) dim srmObj, strData set srmObj = server.CreateObject ( "adodb.stream") srmObj.Type = 1 SRMOBJ.MODE = 3 SRMOBJ.Open Srmsource.position = Posbegin-1 'location count The first count is different, this object is the SRMSource.copyto Srmobj started to 0 = "GB2312" STRDATA = SRMOBJ.READTEXT SRMOBJ.CLOSE SET SRMOBJ = NOTHING GETTEXTFROMBIN = STRDATA END FUNCTION 'Double-byte string Convert into single-byte string Function GetSbFromDB (ByTString) Dim bin, I bin = "" for i = 1 to Len (ByTString) bin = bin & chr B (ASC (ByTString, I, 1))) Next getsbfromDB = bin end function 'single-byte string converted into double-byte string function getDbfromsb (bitstring) DIM STR, I STR = "" for i = 1 to lenb (bitString) str = str & chr (ascb (midb (bitString, i, 1))) next getDBfromSB = str end function 'precipitated from a full path to the file name in function getFileNamefromPath (strPath) getFileNamefromPath = mid (strPath, instrrev (strpath, "/") 1) end function 'judgment function function IIF (COND, EXPR1, EXPR2) IF COND THEN IIF = EXPR1 ELSE IIF = EXPR2 END IF FUNCTION'</p> <p>Define database connection string DIM CNSTR CNSTR = "driver = {Microsoft Access Driver (* .mdb)}; DBQ =" & Server.mAppath ("./ Upload.mdb")%> <html> <head> <title> Multiple table single field or image Synchronization Save to Database </ Title> <meta http-equiv = "Content-Type" Content = "text / html; charset = GB2312"> </ head> <body> <p> navigation menu: <b> Upload Image </ b> <a href="showimagelistfromData2.asp"> Display Image <HR> <HR> </ P> <% if Request.ServerVariables ("Request_Method") = "post" the Dim Scome , binData dim posB, posE, posSB, posSE dim binCrlf, binSub dim strTitle, strFileName, strContentType, posFileBegin, posFileLen, aryFileInfo dim i, j dim dicData dim strName, strValue binCrlf = getSBfromDB (vbcrlf) 'defines a single byte back Car changing string binsub = getsbfromdb ("-") 'Defines a single byte "-" string set screen = server.createObject ("adodb.stream") Scome.Type = 1' Specify return data type adtypebinary = 1, adTypeText = 2 sCome.Mode = 3 'open mode specified adModeRead = 1, adModeWrite = 2, adModeReadWrite = 3 sCome.Open sCome.Write request.BinaryRead (request.TotalBytes) sCome.Position = 0 binData = sCome.Read' Response.binarywrite bindata 'debugging: display All data submitted 'response.write "<hr>" debugging POSB = INSTRB (Bindata, Binsub) POSB = INSTRB (POSB, BINDATA, BINCRLF) 2' 2 is the length of the carriage return to the line character = INSTRB (POSB, Bindata, GetsbfromDB ("Name =" ")) 6 set Dicdata = Server.createObject (" scripting.dictionary ") 'Used to save information do unTil Posb = 6 pose = INSTRB (POSB, Bindata, GetsbfromDB ("" ")) '</p> <p>Response.write "name =" & getTextFrombin (Scome, POSB, POSE-POSB) & "<br>" Strname = getTextFrombin (Scome, POSB, POSE-POSB) POSB = POSE 1 'pointer moves to "" " POSE = INSTRB (POSB, BINDATA, BINCRLF) 'response.write POSB & "->" & Pose & "<br>" IF INSTRB (MIDB (Bindata, POSB, POSE-POSB), GetsbfromDB ("FileName =" " ))))> 0 THEN 'Judgment Success Demodes this is a File domain POSB = INSTRB (POSB, Bindata, GetsbfromDB ("FileName =" ")) 10 Pose = INSTRB (POSB, Bindata, GetsbFromDB (" "")) If POSB THEN 'RESPONSE.WRITE "FileName =" & GetTextFrombin (Scome, POSB, POSE-POSB) & "<br>" strfilename = getFileNameFromPath (GetTextFrombin) POSB = INSTRB (POSB , Bindata, GetsbfromDB ("Content-Type:")) 14 POSE = INSTRB (POSB, BINDATA, BINCRLF) 'response.write "Content-Type =" & GetTextFrombin (Scome, POSB, POSE-POSB) & "<br > "StrContentType = getTextFrombin (Scome, POSB, POSE-POSB) POSB = POSE 4 'This place changed two lines, and the original binary data for the output is pose = INSTRB (POSB, BINDATA, BINSUB)' response.write" Image Data = "'Savebin2File Scome, POSB, POSE-POSB-1, Server.mappath (".) & "/Test.jpg"' response.write "<img src = 'test.jpg' border = 0> <br> "POSFILEBEGIN = POSB POSFILEN = Pose-POSB-1 Strval = Strfilename &", "& StrContentType &", "& Posfile Segin &", "& PosFilelen Else"</p> <p>Response.write "No Uploaded Document!" & "<br>" Strvalue = "" END IF ELSE POSB = POSE 4 'This place changed two lines, the original binary data for the output is pose = INSTRB (POSB, Bindata, BinCrlf) 'response.write "value =" & gettextFrombin (Scome, POSB, POSE-POSB) & "<br>" This is not "" ", so no -1 strval = getTextFrombin (Scomeb, POSB, POSE-POSB ) End if Dicdata.add strname, strval posb = pose 2 POSB = INSTRB (POSB, BINDATA, BINCRLF) 2 POSB = INSTRB (POSB, BINDATA, GETSBFROMDB ("Name =" ")) 6 loop startle = Dicdata .Item ( "txtTitle") aryFileInfo = dicData.Item ( "filImage") if aryFileInfo <> "" then 'has the file data upload aryFileInfo = split (aryFileInfo, ",") strFileName = aryFileInfo (0) strContentType = aryFileInfo (1 ) POSFILEBEGIN = aryfileinfo (2) POSFILEN = aryfileinfo (3) scrolls = POSFILEBEGIN-1 Bindata = SCOME.READ (POSFILEN) DIM CN, RS, SQL SET CN = Server.createObject ("AdoDb.Connection") CN.Open CNSTR SET RS = Server.createObject ("AdoDb.Recordset") SQL = "SELECT TITLE, [C Ontent-type], image from tblimage2 "RS.Open SQL, CN, 1, 3 rs.addnew rs.fields (" Title "). Value = strtitle rs.fields (" content-type "). Value = strcontenttype 'data Save to Database Rs.fields ("Image"). Appendchunk Bindata 'If the data is saved on the disk in the file, use the following' savebin2file scome, POSFILEBEGIN, POSFILEN, Server.MAppath ("./") & strfilename RS. Update rs.close set = Nothing cn.close set cn =</p> <p>Nothing response.write "<p> file saved successfully! </ p>" else response.write "<p> No uploaded file! </ p>" end if scrings.close set screen = nothing else%> <form id = "frmupload" name = "frmupload" action = "<% = request.servervariables (" script_name ")%>" method = "post" target = "_ self" enctype = "multipart / form-data"> <p> title: <input id = "txtttitle" type = "text" name = "txttitle" size = "40"> </ p> <p> picture: <input = "filimage" type = "file" name = "filimage" Size = "40"> </ p> <input = "btnupload" type = "submit" value = "upload" name = "btnupload"> </ form> <% end if%> </ body> </ html> Renewal ... Author related articles: ASP file uploads the first weight of the power (seeing the graphics) (formerly) The creative design of the website (Favorites) Meta label (Favorites)</p> <p>Other related articles: Upload Download with JSPSmartupload (Original) Using Smartupload Components Using Smartupload Components in JSP (Original) Using Smartupload Components Upload File (Original) Implement Multi-File Upload (Original) in ASP.NET (original) Upload (original)</p> <p>Comments on this article</p> <p><script src = "/ wevelop / readnum.asp? id = 19253> </ script> Popularity: 1114</p> <p>Inancial (2003-6-26 23:02:08) Glenglenglen: Yes, change to this better, because of good hard, the code may not be optimized, thank you for your fingers. MRRDH007: You look at the first weight, the STREAM object's type = 1 indicates that the returned data is binary data, = 2 is text data, mode = 1 represents readable, = 2 indicates writable, = 3 Indicates that you can read or write.</p> <p>MRRDH007 (2003-6-26 18:46:48) SRMOBJ.TYPE = 1srmobj.mode = 3 asked, what does this mean? Don't understand</p> <p>Glenglenglen (2003-6-26 17:11:27) GetFileNameFromPath (STRPATH) function doesn't change to Right?</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-25089.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="25089" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.040</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'ZxmKtkONNCtKIqBJgBwMRebdeRoRniij7pmawBt4WAa9htTAtYn2LRmpDJ_2FJv2zLhzmidjWjtHKZweqRJ1CcdQ_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>