How to build your own upload assembly programming ideas

zhaozj2021-02-16  64

Some information collected before - how to build our programming ideas

Keywords: ASP

In the last post, I mentioned several comparisons for uploading components, now we do your own, full of bags, to build your own uploading components, this uploading component should have the following functions: 1. You should be able to accept the values ​​passed in the FORM element of the various HTMLs without knowing that it is passing through Text or SELECT. An upload path 3 should be given. You should be able to limit the size of the upload file 4. You should be able to support multiple files simultaneously upload 5. Abnormal error 6 should be handled. It should be capable of stabilizing 7. It should be able to be able to operate with this thin, and can be operated in IE and Netscape at the same time) 8. Ability to save files in the database 9. It should be able to limit the user rights code and files as shown below (old rules, I don't explain the detailed explanation) 1. Upload.htm Upload </ title> </ head> <body> <form name = "frmupload" method = "post" eNCTYPE = "Multipart / form-data" action = "Upload. ASP "> <Table> <Tr> <TD> <TD> <TD> <input type =" text "name =" txtauthor "> </ td> </ tr> <tr> <td> file </ TD> <TD> <input type = "file" name = "txtFileName"> </ td> </ tr> <tr> <td color> "2" align = "right"> <input type = "submit" Value = "Upload"> </ td> </ tr> </ table> </ form> </ body> </ html> ** Note: Using ENCTYPE = "Multipart / Form-Data" is to allow Form to submit one Document 2. Upload.asp <% @ Language = VBScript%> <% Option explicitResponse.Buffer = TrueOn Error Resume NextIf Request.ServerVariables ( "REQUEST_METHOD") = "POST" Then Dim objUpload Dim lngMaxFileBytes Dim strUploadPath Dim varResult lngMaxFileBytes = 10000 strUploadPath = "c : / inetpub / wwwroot / upload / "set objupload = server.createObject (" pjuploadfile.clsupload ") If Err.Number <> 0 The Response.write component is not installed correctly.</p> <p>"Else Varresult = Objupload.doupload (LNGMAXFILEBYTES, STRUPLOADPATH) SET OBJUPLOAD = Nothing Dim i for i = 0 To Ubound (Varresult, 1) Response.write Varresult (i, 0) &": "& VarResult (i, 1) & "<br>" NEXT END IFEEND IF%> Now develop this ActiveX control now: (Be noteworthy, because I am lazy, some code may be incomplete, but it is important to understand the programming idea of ​​this component) 1 Quote Active Server Pages Object Library. 2.</p> <p>Code is as follows: Option ExplicitPrivate MyScriptingContext As ScriptingContextPrivate MyRequest As RequestPrivate MyResponse As RequestPrivate Const ERR_NO_FILENAME As Long = vbObjectError 100Private Const ERR_NO_EXTENSION As Long = vbObjectError 101Private Const ERR_EMPTY_FILE As Long = vbObjectError 102Private Const ERR_FILESIZE_NOT_ALLOWED As Long = vbObjectError 103Private Const ERR_FOLDER_DOES_NOT_EXIST As Long = vbObjectError 104Private Const ERR_FILE_ALREADY_EXISTS As Long = vbObjectError 105Public Sub OnStartPage (PassedScriptingContext As ScriptingContext) Set MyScriptingContext = PassedScriptingContext Set myRequest = MyScriptingContext.Request Set myResponse = MySriptingContext.ResponseEnd SubPrivate Function GetFileName (strFilePath) As String Dim intPos As Integer GetFileName = strFilePath For intpos = len (strfilepath) to 1 step -1 if MID (StrfilePath, INTPOS, 1) = "/" or MID (StrfilePath, INTPOS, 1) = ":" "THEN GETFILENAME = Right (STRFI lePath, Len (strFilePath) - intPos) Exit Function End If Next End FunctionPrivate Function CheckFileExtension (strFileName) As Boolean Dim strFileExtension As String If InStr (strFileName,) Then strFileExtension = Mid (strFileName, InStrRev (strFileName, "." "." ) 1) if len (strfileExtension) <3 Ten CheckfileExtension = false else checkfileextension = true end if else checkfileextension =</p> <p>False End If End FunctionPrivate Sub WriteFile (ByVal strUploadPath As String, ByVal strFileName As String, _ ByVal lngFileLength As Long) End SubPublic Function DoUpload (ByVal lngMaxFileBytes As Long, _ ByVal strUploadPath As String) As Variant Dim varByteCount As Variant Dim varHTTPHeader As Variant Dim lngFileLength As Long Dim arrError (0, 1) As Variant On Error GoTo DoUpload_Err varByteCount = MyRequest.TotalBytes varHTTPHeader = StrConv (MyRequest.BinaryRead (varByteCount), vbUnicode) MyResponse.Write varHTTPHeader Dim intFormFieldCounter As Integer intFormFieldCounter = Len (varHTTPHeader) - len (Replace (varHTTPHeader, "; name =", Mid ( "; name =", 2))) ReDim arrFormFields (intFormFieldCounter - 1, 1) As Variant For i = 0 To intFormFieldCounter - 1 lngFormFieldNameStart = InStrB (lngFormFieldNameStart 1 , VARHTTPHEADER, "; Name =" & chr (34)) LNGFORMFIELDNAMEEND = INSTRB (LngFormfieldNamestart _ LEN (StrConv (" name = "& Chr (34), vbUnicode)), varHTTPHeader, Chr (34)) _ Len (StrConv (Chr (34), vbUnicode)) strFormFieldName = MidB (varHTTPHeader, lngFormFieldNameStart, lngFormFieldNameEnd - lngFormFieldNameStart) strFormFieldName = Replace ( strFormFieldName, "; name =", vbNullString) strFormFieldName = Replace (strFormFieldName, Chr (34), vbNullString) If MidB (varHTTPHeader, lngFormFieldNameEnd, 2) = ";" Then lngFormFieldValueStart = InStrB (lngFormFieldNameEnd, varHTTPHeader, "filename =" & CHR (34)) LNGFORMFIELDVALUEEND =</p> <p>InStrB (lngFormFieldValueStart Len (StrConv ( "filename =" & Chr (34), vbUnicode)), varHTTPHeader, Chr (34)) strFileName = MidB (varHTTPHeader, lngFormFieldValueStart, lngFormFieldValueEnd - lngFormFieldValueStart) strFileName = Mid (strFileName, InStr (strFileName , "=") 2, Len (strFileName) - InStr (strFileName, "=")) strFileName = Replace (strFileName, Chr (34), vbNullString) Else lngFormFieldValueStart = lngFormFieldNameEnd lngFormFieldValueEnd = InStrB (lngFormFieldValueStart, varHTTPHeader, varDelimeter) strFormFieldValue = MidB (varHTTPHeader, lngFormFieldValueStart, lngFormFieldValueEnd - lngFormFieldValueStart) strFormFieldValue = Replace (strFormFieldValue, vbCrLf, vbNullString) lngFormFieldNameStart = lngFormFieldValueEnd End If arrFormFields (i, 0) = strFormFieldName arrFormFields (i, 1) = strFormFieldValue strFileName = GetFileName (strFileName) If Len (strFileName) = 0 Then Err.Raise ERR_NO_FILENAME End If If Not CheckFileExtension (strFileName) Then Err.Raise ERR_NO_EXTENSION End If lngFileDataStart = InStr (InStr (varHTTPHeader, strFileName), varHTTPHeader, vbCrLf & vbCrLf) 4 lngFileDataEnd = InStr ( lngFileDataStart, varHTTPHeader, varDelimeter) lngFileLength = lngFileDataEnd-lngFileDataStart If lngFileLength <= 2 Then Err.Raise ERR_EMPTY_FILE End If If Not lngMaxFileBytes = 0 Then If lngMaxFileBytes <</p> <p>lngFileLength Then Err.Raise ERR_FILESIZE_NOT_ALLOWED End If End If If Not fs.FolderExists (strUploadPath) Then Err.Raise ERR_FOLDER_DOES_NOT_EXIST End If If fs.FileExists (strUploadPath & strFileName) Then Err.Raise ERR_FILE_ALREADY_EXISTS End If Set sFile = fs.CreateTextFile (strUploadPath & strFileName, True) sFile.Write varContent, lngFileDataStart, lngFileLength Close File sFile.Close Set sFile = Nothing Set fs = Nothing Next DoUpload = "" Exit FunctionDoUpload_Err: arrError (0, 0) = "Error" Select Case Err.Number Case ERR_NO_FILENAME Arrerror (0, 1) = "No input to the file name you need to submit." Case err_no_extension Arrerror (0, 1) = "file extension error." Case Err_empty_file arrerror (0, 1) = "The length of the file you want to upload is 0. "Case Err_FILESIZE_NOT_ALLOWED Arrerror (0, 1) =" Total Upload [& lngFileLength & _ "] bytes exceeds the maximum allowable requirements [" & _ LNGMAXFILEBYTES & "]." "CASE ERR_FOLDER_DOES_NOT_EXIST Arrerror (0, 1) =" Uploaded directory does not exist. "CASE ERR_FILE_ALREADY_EXISTS Arrerror (0, 1) =" file ["& strfilename &"] already exists. "Case Else Arrerror (0, 1) = err.description end select dolpload = arrerror () End function Some information for previously collected components - related comparisons and descriptions</p> <p>Keywords: ASP</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-15593.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="15593" 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 = 'gM9Obdlmy2wKS7pTiqeBM45XUFWZQ2ek2zBYsBQQwT77wRDf7EybNGezggUZWTdITzTYDbEtvJn6OnxXIIoGSw_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>