• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Upload file to file.io using POST method

YasserKhalil

Well-Known Member
Hello everyone

I have found a link at SO that may make difference at this query
https://stackoverflow.com/questions/50110601/upload-a-picture-to-file-io-http-post-in-vba
The code from this link
Code:
Sub UploadFilesUsingVBAORIGINAL()
     'this proc will upload below files to https://file.io/
          '  png, jpg, txt

        Dim fileFullPath As String
        fileFullPath = ThisWorkbook.Path & "\Sample.txt"

        POST_multipart_form_dataO fileFullPath
    End Sub

Private Function GetGUID() As String
    ' Generate uuid version 4 using VBA
    GetGUID = WorksheetFunction.Concat(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_dataO(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", LCase(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())
        Debug.Print .responseText
    End With

End Sub

Anyone can try this code as the website is for free. When I run the code, I got "Success" in the immediate window and got a link to the uploaded file.
This appears to have no problem but when taking the link and put it in a browser, I got 404 Page not found

I tried uploading the same file manually and it works well without any problem as for the link I got from this manual steps

Any help please?
 
Yes but this time I have a code that gives me success in the immediate window. But when taking the URL generated and pasted in the browser I found empty content. Just need to detect the fault in the code
Is the fault in the POST of form data or the contents of the text file (when converted to binary)?
 
I tried to contact him but it seems he is unavailable. Can you please help me with this and fix things you dislike? In fact, I want to learn more than to solve the issue.
If you can give me an example of how uploading on any website with the VBA not using CURL ..
 
I have nothing else new since your previous thread and as all the examples failed …​
It can not be 'on any website' as each time many things depend specificly on each site, so can't be generic.​
And since two days I have web issues on my side, disconnected, no more access to internet for half a day,​
so I do not have much time for this and as you can use Curl like Chihiro showed you …​
 
Thanks a lot for the support. I will try to review the links from the previous thread. Maybe I can get a clue
 
I am trying to post simple text .. no files now
This is my try but return error
Code:
Sub POST_Method_Example()
    Dim myMessage As String

    myMessage = "YasserKhalil Chandoo"

    With CreateObject("MSXML2.ServerXMLHTTP")
        .Open "POST", "https://file.io/"

        .setRequestHeader "Content-Type", "multipart/form-data; boundary=data"
        .send (myMessage)
        Debug.Print .ResponseText
    End With
End Sub
 
Back
Top