0

I am trying to export a text file from my local drive to the mongo DB.

With the below code, it is showing successful after executing, and the file is successfully created in the mongo DB.

But when I download the file back , I can see that it does not contain any data , and the size is 0 KB.

Tried with many options, but no luck .

below is the code which i used.

Const PATH = "C:\Test\600001453\"
Const FILENAME = "erstudio_drone_operations_primary.dm1_600001453.txt"
'Const FILENAME1 = "Droned.docx"
Const CONTENT = "text/plain"
Const URL = "https://datafinder-e1.test.com/test/?repository=testrepo=Test    model.DM1&carid=00000&repository=testrepo&modelname=test&submodelname=test"

' generate boundary
Dim BOUNDARY, s As String, n As Integer
For n = 1 To 16
    
 s = s & Chr(65 + Int(Rnd * 25))
 Next
'BOUNDARY = s & CDbl(Now)
BOUNDARY = "---------------------------0123456789012"
Dim part As String, ado As Object

Dim part1 As String


Dim header As String

Dim FILE, FILESIZE
Set ado = CreateObject("ADODB.Stream")

ado.Type = 1 'binary
ado.Open
ado.LoadFromFile PATH & FILENAME
ado.Position = 0
FILESIZE = ado.Size
FILE = ado.Read
ado.Close



    
part = "--" & BOUNDARY & vbCrLf
part = part & "Content-Disposition: form-data; name=""ddl""; filename=""" & FILENAME & """" & vbCrLf
part = part & "Content-Type: " & CONTENT & vbCrLf
part = part & "Content-Length: " & FILESIZE & vbCrLf & vbCrLf & vbCrLf
part = part & "--" & BOUNDARY & "--" & vbCrLf


header = "Content-Type" & ": " & "multipart/form-data; boundary=" & BOUNDARY

' combine part, fl , end
ado.Open
ado.Position = 0
ado.Type = 1 ' binary
ado.Write ToBytes(part)
ado.Write FILE
ado.Write ToBytes(vbCrLf & "--" & BOUNDARY & "---")
ado.Position = 0
'Debug.Print ado.Size


With CreateObject("MSXML2.ServerXMLHTTP")
    .Open "POST", URL, False
    .SetRequestHeader "Content-Type", "multipart/form-data; boundary=" & BOUNDARY
    .Send ado.Read
    ado.Close
    Debug.Print .ResponseText
End With

End Sub

Function ToBytes(str As String) As Variant

Dim ado As Object
Set ado = CreateObject("ADODB.Stream")
ado.Open
ado.Type = 2 ' text
ado.Charset = "_autodetect"
ado.WriteText str
ado.Position = 0
ado.Type = 1
ToBytes = ado.Read
ado.Close

End Function

I can see the successful message , and the file is copied to the mongo dB. But the content is missing , and the file size is 0 kb

FunThomas
  • 23,043
  • 3
  • 18
  • 34
Arun
  • 1

0 Answers0