Upload a file via <input \\input> in HTML form with VBA

别来无恙 提交于 2019-12-05 10:35:30

You can use ScriptUtils.ASPForm to accept uploaded files in ASP. ScriptUtils.ASPForm contains hi-performance, low resources consumption algorithm which can accept up to 2GB of data.

  1. There are some steps to upload file using http and multipart/form-data document. First of all we have to read file from a 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.

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

  3. Last task is send the multipart/form-data document as a post request to server with multipart/form-data Content-Type header. We can use at least two object to send POST request - XMLHttp or InternetExplorer. This script uses Navigate method of InternetExplorer.Application object. You can see the code in IEPostBinaryRequest function

please look into the below link for more information.

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

GetFile method is converting the file to UTF-8. Pdf will have more than 128 byte, you need to convert it to multi byte string

'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 this page

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

Rich Moss

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

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

This page indicates he had some success with the XMLHTTP object that allows finer control over the HTTP request. Here's 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) 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!