Uploading File To Website

2019-04-11 09:24发布

I am still a beginner with VBA, so if you need further explanation or I am not describing my problem correctly please let me know.

I am trying to achieve the following: Upload a file from my computer to a website (You can only try to upload if you login, so sadly I cannot share a link)

In order to achieve this I need to do three things: 1) Click the "Upload" Button 2) Insert the Filepath into the search field of the PopUp Window 3) Click the "Open" Button

The website looks like this: enter image description here

The PopUp Window looks like this: enter image description here

The HTML code of the upload field is the following:

<div class="button-wrapper">
    <input class="design-file-input" type="file">
    <a class=" button prio1" href="javascript:void(0);">Design hochladen</a>
</div>

I guess there might be two solutions two my problem, however, I am not able to realize my plans.

IDEA 1

Somehow get the filepath into the input field and the page to download it Therefore I tried the vba following VBA codes:

objIE.document.getElementsByClassName("design-file-input")(0).Value
objIE.document.getElementsByClassName("design-file-input")(0).innerText

And then try to somehow make the website submit my entry.

IDEA 2

Click the "Design Hochladen" Button.

objIE.document.getElementsByClassName("button-wrapper")(0).Click

But then the PopUp window comes up and I don't know how to control it with VBA

I am happy to hear and try your suggestions!! If you need any further details, just let me know! Thank you so much if you can give me any advice

1条回答
甜甜的少女心
2楼-- · 2019-04-11 10:02

Directly assigning the file path to the value of that specific HTML element does not work. A while ago, I had the same issue (automatically passing a file to an upload file dialog). After a long googling session, I found following solution. Unfortunately, I could not find the link from where I took this answer. In case I come accross the website, I will share the link with you:

Dim FilePath As String
Dim FileName As String
FilePath = Environ$("temp") & "\"
FileName = "test_file_for_upload" & ".xlsx"

UploadFile DestURL, FilePath & FileName, "file" 'Usage

'******************* upload - begin
'Upload file using input type=file
Public Sub UploadFile(DestURL As String, FileName As String, _
  Optional ByVal FieldName As String = "File")
  Dim sFormData As String, d As String

  'Boundary of fields.
  'Be sure this string is Not In the source file
  Const Boundary As String = "---------------------------0123456789012"

  'Get source file As a string.
  sFormData = GetFile(FileName)

  'Build source form with file contents
  d = "--" + Boundary + vbCrLf
  d = d + "Content-Disposition: form-data; name=""" + FieldName + """;"
  d = d + " filename=""" + FileName + """" + vbCrLf
  d = d + "Content-Type: application/upload" + vbCrLf + vbCrLf
  d = d + sFormData
  d = d + vbCrLf + "--" + Boundary + "--" + vbCrLf

  'Post the data To the destination URL
  IEPostStringRequest DestURL, d, Boundary
End Sub

'sends URL encoded form data To the URL using IE
Sub IEPostStringRequest(URL As String, FormData As String, Boundary As String)
  'Create InternetExplorer
  Dim WebBrowser: Set WebBrowser = CreateObject("InternetExplorer.Application")

  'You can uncoment Next line To see form results
  WebBrowser.Visible = True

  'Send the form data To URL As POST request
  Dim bFormData() As Byte
  ReDim bFormData(Len(FormData) - 1)
  bFormData = StrConv(FormData, vbFromUnicode)

  WebBrowser.navigate URL, , , bFormData, _
    "Content-Type: multipart/form-data; boundary=" + Boundary + vbCrLf

  Do While WebBrowser.Busy
'    Sleep 100
    DoEvents
  Loop
  'WebBrowser.Quit
End Sub

'read binary file As a string value
Function GetFile(FileName As String) As String
  Dim FileContents() As Byte, FileNumber As Integer
  ReDim FileContents(FileLen(FileName) - 1)
  FileNumber = FreeFile
  Open FileName For Binary As FileNumber
    Get FileNumber, , FileContents
  Close FileNumber
  GetFile = StrConv(FileContents, vbUnicode)
End Function
'******************* upload - end

The third argument "file" denotes the ID of the HTML element which needs to be triggered. Hope this solution workds for you as well

查看更多
登录 后发表回答