Upload file via <input \ input> in HTML format using VBA

I am trying to upload a photo file to OCR Site , however the PDF file does not upload to the site.

To do this, I use the following code, and the HTML segment below:

Sub DownPDF() Dim FileName As String: FileName = "C:\Users\310217955\Documents\pdfdown\SGSSI001_HL1464_2011.pdf" Dim DestURL As String: DestURL = "https://www.newocr.com/" Dim FieldName As String: FieldName = "userfile" Call UploadFile(DestURL, FileName, FieldName) End Sub '******************* upload - begin 'Upload file using input type=file Sub UploadFile(DestURL, FileName, FieldName) 'Boundary of fields. 'Be sure this string is Not In the source file Const Boundary = "---------------------------0123456789012" Dim FileContents, FormData 'Get source file As a binary data. FileContents = GetFile(FileName) 'Build multipart/form-data document FormData = BuildFormData(FileContents, Boundary, FileName, FieldName) 'Post the data To the destination URL IEPostBinaryRequest DestURL, FormData, Boundary End Sub 'Build multipart/form-data document with file contents And header info Function BuildFormData(FileContents, Boundary, FileName, FieldName) Dim FormData, Pre, Po Const ContentType = "application/upload" 'The two parts around file contents In the multipart-form data. Pre = "--" + Boundary + vbCrLf + mpFields(FieldName, FileName, ContentType) Po = vbCrLf + "--" + Boundary + "--" + vbCrLf 'Build form data using recordset binary field Const adLongVarBinary = 205 Dim RS: Set RS = CreateObject("ADODB.Recordset") RS.Fields.Append "b", adLongVarBinary, Len(Pre) + LenB(FileContents) + Len(Po) RS.Open RS.AddNew Dim LenData 'Convert Pre string value To a binary data LenData = Len(Pre) RS("b").AppendChunk (StringToMB(Pre) & ChrB(0)) Pre = RS("b").GetChunk(LenData) RS("b") = "" 'Convert Po string value To a binary data LenData = Len(Po) RS("b").AppendChunk (StringToMB(Po) & ChrB(0)) Po = RS("b").GetChunk(LenData) RS("b") = "" 'Join Pre + FileContents + Po binary data RS("b").AppendChunk (Pre) RS("b").AppendChunk (FileContents) RS("b").AppendChunk (Po) RS.Update FormData = RS("b") RS.Close BuildFormData = FormData End Function 'sends multipart/form-data To the URL using IE Function IEPostBinaryRequest(URL, FormData, Boundary) 'Create InternetExplorer Dim IE: Set IE = CreateObject("InternetExplorer.Application") 'You can uncoment Next line To see form results IE.Visible = True 'Send the form data To URL As POST multipart/form-data request IE.Navigate URL, , , FormData, _ "Content-Type: multipart/form-data; boundary=" + Boundary + vbCrLf Do While IE.Busy Or IE.readyState <> 4 Wait 1, "Upload To " & URL Loop 'Get a result of the script which has received upload On Error Resume Next IEPostBinaryRequest = IE.document.body.innerHTML 'IE.Quit End Function 'Infrormations In form field header. Function mpFields(FieldName, FileName, ContentType) Dim MPTemplate 'template For multipart header MPTemplate = "Content-Disposition: form-data; name=""{field}"";" + _ " filename=""{file}""" + vbCrLf + _ "Content-Type: {ct}" + vbCrLf + vbCrLf Dim Out Out = Replace(MPTemplate, "{field}", FieldName) Out = Replace(Out, "{file}", FileName) mpFields = Replace(Out, "{ct}", ContentType) End Function Sub Wait(Seconds, Message) On Error Resume Next CreateObject("wscript.shell").Popup Message, Seconds, "", 64 End Sub 'Returns file contents As a binary data Function GetFile(FileName) Dim Stream: Set Stream = CreateObject("ADODB.Stream") Stream.Type = 1 'Binary Stream.Open Stream.LoadFromFile FileName GetFile = Stream.Read Stream.Close End Function 'Converts OLE string To multibyte string Function StringToMB(S) Dim I, B For I = 1 To Len(S) B = B & ChrB(Asc(Mid(S, I, 1))) Next StringToMB = B End Function '******************* upload - end '******************* Support 'Basic script info Sub InfoEcho() Dim Msg Msg = Msg + "Upload file using http And multipart/form-data" & vbCrLf Msg = Msg + "Copyright (C) 2001 Antonin Foller, PSTRUH Software" & vbCrLf Msg = Msg + "use" & vbCrLf Msg = Msg + "[cscript|wscript] fupload.vbs file url [fieldname]" & vbCrLf Msg = Msg + " file ... Local file To upload" & vbCrLf Msg = Msg + " url ... URL which can accept uploaded data" & vbCrLf Msg = Msg + " fieldname ... Name of the source form field." & vbCrLf Msg = Msg + vbCrLf + CheckRequirements WScript.Echo Msg WScript.Quit End Sub 'Checks If all of required objects are installed Function CheckRequirements() Dim Msg Msg = "This script requires some objects installed To run properly." & vbCrLf Msg = Msg & CheckOneObject("ADODB.Recordset") Msg = Msg & CheckOneObject("ADODB.Stream") Msg = Msg & CheckOneObject("InternetExplorer.Application") CheckRequirements = Msg ' MsgBox Msg End Function 'Checks If the one object is installed. Function CheckOneObject(oClass) Dim Msg On Error Resume Next CreateObject oClass If Err = 0 Then Msg = "OK" Else Msg = "Error:" & Err.Description CheckOneObject = oClass & " - " & Msg & vbCrLf End Function 

Here is an HTML snippet.

 <input name="userfile" id="userfile" type="file"> 
+5
source share
2 answers

You can use ScriptUtils.ASPForm to accept downloaded files in ASP. ScriptUtils.ASPForm contains a high-performance, low-resource algorithm that can accept up to 2 GB of data.

  • There are several steps to upload a file using the http document and multipart / form-data. First of all, we need to read the file from disk. We can use Scripting.FileSystemObject to read text data or ADODB.Stream to read any file. The GetFile function does the work using ADODB.Stream.

  • The second task we need to complete is the assembly of the multipart / form-data document. The document contains several fields separated by a border. Each of the fields has its own header, which contains information about the field name, file name and content type of the source file. The ADO Recordset object has an excellent AppendChunk method that allows you to join parts of a multipart / form-data document (open border + headers + file contents + close border). You can see the code in the BuildFormData function.

  • The final task is to send the multipart / form-data document as an email request to the server with the Content-Type header with support for multipart / form-data. We can use at least two objects to send a POST request - XMLHttp or InternetExplorer. This script uses the move method of the InternetExplorer.Application object. You can see the code in the IEPostBinaryRequest function

Check out the link below for more information.

http://www.motobit.com/tips/detpg_uploadvbsie/

The GetFile method converts the file to UTF-8. Pdf will have more than 128 bytes, you need to convert it to a string with several bytes

 'Converts OLE string To multibyte stringFunction StringToMB(S) Dim I, B For I = 1 To Len(S) B = B & ChrB(Asc(Mid(S, I, 1))) Next StringToMB = B End Function 

Please refer to this page.

http://www.mrexcel.com/forum/excel-questions/861695-using-xmlhttp-upload-file-api.html#post4192153

+1
source

I spent several days experimenting with the same method using the Navigate method of the InternetExplorer.Application COM interface to download the file.
The documentation for Navigate indicates that specifying the postdata parameter will result in an HTTP POST, but in my experience, Content-Type is also a determining factor. Using Fiddler, I found that it consistently sent a GET HTTP method instead of a POST when Content-Type = multipart / form-data.

Sending a GET verb will tell the server to ignore any form data and only process URIs.

This page shows that it has had some success with the XMLHTTP object, which allows more precise control of the HTTP request. Here is some Powershell code that demonstrates this technique:

 $http = (New-Object -ComObject "MSXML2.XMLHTTP") $http.Open("POST",$DestURL,$false) $http.SetRequestHeader("Content-Type", "multipart/form-data; boundary=" + $boundary) $http.Send($PostData) 
0
source

Source: https://habr.com/ru/post/1234172/


All Articles