paun_shotts
New Member
- Joined
- Nov 4, 2021
- Messages
- 41
- Office Version
- 2013
- Platform
- Windows
Hi,
I have some VBA code that uploads a file to files.io, I am then given a response, which includes the download link.
The issue is that when I download the file, the image cannot be viewed by any app that I have tried.
I tried uploading a .txt file, and when I downloaded the file, it was blank.
I have tried .jpeg .jpg .txt but none of the files are able to be viewed properly after downloading.
Here is my code:
I have some VBA code that uploads a file to files.io, I am then given a response, which includes the download link.
The issue is that when I download the file, the image cannot be viewed by any app that I have tried.
I tried uploading a .txt file, and when I downloaded the file, it was blank.
I have tried .jpeg .jpg .txt but none of the files are able to be viewed properly after downloading.
Here is my code:
VBA Code:
Sub UploadFilesUsingVBA()
'this proc will upload below files to https://file.io/
' png, jpg, txt
Dim fileFullPath As String
fileFullPath = "C:\Users\shaun\Documents\Sports Betting\NFL\Daily Bets\20231114.png"
POST_multipart_form_data fileFullPath
End Sub
Private Function GetGUID() As String
' Generate uuid version 4 using VBA
GetGUID = WorksheetFunction.Dec2Hex(WorksheetFunction.RandBetween(0, 4294967295#), 8) & "-" & _
WorksheetFunction.Dec2Hex(WorksheetFunction.RandBetween(0, 65535), 4) & "-" & _
WorksheetFunction.Dec2Hex(WorksheetFunction.RandBetween(16384, 20479), 4) & "-" & _
WorksheetFunction.Dec2Hex(WorksheetFunction.RandBetween(32768, 49151), 4) & "-" & _
WorksheetFunction.Dec2Hex(WorksheetFunction.RandBetween(0, 65535), 4) & _
WorksheetFunction.Dec2Hex(WorksheetFunction.RandBetween(0, 4294967295#), 8)
End Function
Private Function GetFileSize(fileFullPath As String) As Long
Dim lngFSize As Long, lngDSize As Long
Dim oFO As Object, OFS As Object
lngFSize = 0
Set OFS = CreateObject("Scripting.FileSystemObject")
If OFS.FileExists(fileFullPath) Then
Set oFO = OFS.getFile(fileFullPath)
GetFileSize = oFO.Size
Else
GetFileSize = 0
End If
Set oFO = Nothing
Set OFS = Nothing
End Function
Private Function ReadBinary(strFilePath As String)
Dim ado As Object, bytFile
Set ado = CreateObject("ADODB.Stream")
ado.Type = 1
ado.Open
ado.LoadFromFile strFilePath
bytFile = ado.Read
ado.Close
ReadBinary = bytFile
Set ado = Nothing
End Function
Private Function toArray(str)
Dim ado As Object
Set ado = CreateObject("ADODB.Stream")
ado.Type = 2
ado.Charset = "_autodetect"
ado.Open
ado.WriteText (str)
ado.Position = 0
ado.Type = 1
toArray = ado.Read()
Set ado = Nothing
End Function
Sub POST_multipart_form_data(filePath As String)
Dim oFields As Object, ado As Object
Dim sBoundary As String, sPayLoad As String, GUID As String
Dim fileType As String, fileExtn As String, fileName As String
Dim sName As Variant
fileName = Right(filePath, Len(filePath) - InStrRev(filePath, "\"))
fileExtn = Right(filePath, Len(fileName) - InStrRev(fileName, "."))
Select Case fileExtn
Case "png"
fileType = "image/png"
Case "jpg"
fileType = "image/jpeg"
Case "txt"
fileType = "text/plain"
End Select
Set oFields = CreateObject("Scripting.Dictionary")
With oFields
.Add "qquuid", GetGUID
.Add "qqtotalfilesize", GetFileSize(filePath)
End With
sBoundary = String(27, "-") & "7e234f1f1d0654"
sPayLoad = ""
For Each sName In oFields
sPayLoad = sPayLoad & "--" & sBoundary & vbCrLf
sPayLoad = sPayLoad & "Content-Disposition: form-data; name=""" & sName & """" & vbCrLf & vbCrLf
sPayLoad = sPayLoad & oFields(sName) & vbCrLf
Next
sPayLoad = sPayLoad & "--" & sBoundary & vbCrLf
sPayLoad = sPayLoad & "Content-Disposition: form-data; name=""file""; " & "filename=""" & fileName & """" & vbCrLf
sPayLoad = sPayLoad & "Content-Type: " & fileType & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf
sPayLoad = sPayLoad & "--" & sBoundary & "--"
Set ado = CreateObject("ADODB.Stream")
ado.Type = 1
ado.Open
ado.Write toArray(sPayLoad)
ado.Write ReadBinary(filePath)
ado.Position = 0
With CreateObject("MSXML2.ServerXMLHTTP")
.Open "POST", "https://file.io", False
.SetRequestHeader "Content-Type", "multipart/form-data; boundary=" & sBoundary
.Send (ado.Read())
'MsgBox .responseText
Debug.Print .responseText
End With
End Sub