Topic: IMPORT_MESSAGES_ERROR: files collection is empty ... when import .wln
Hi, I am using VB.NET to import a zip file with messages and I get this error:
{"error":4, "reason":"IMPORT_MESSAGES_ERROR: files collection is empty"}
Here is my code:
unitID is the unit id, and filePathis the full path to zip file.
Dim s As HttpWebRequest
Dim boundary As String = "----WebKitFormBoundary" + DateTime.Now.Ticks.ToString("x")
Dim urlx As String = Wialon_WS_URL + "?svc=exchange/import_messages&sid=" + WialonID
s = HttpWebRequest.Create(urlx)
s.Method = "POST"
s.ContentType = "multipart/form-data; boundary=" + boundary
s.Accept = "Accept-Encoding: gzip,deflate,sdch"
s.KeepAlive = True
s.AllowWriteStreamBuffering = True
Dim memStream As Stream = New MemoryStream()
Dim boundarybytes = Encoding.ASCII.GetBytes(vbCrLf & "--" & boundary & vbCrLf)
Dim endBoundaryBytes = Encoding.ASCII.GetBytes(vbCrLf & "--" & boundary & "--")
Dim formdataTemplate As String = vbCrLf & "--" & boundary & vbCrLf & "Content-Disposition: form-data; name=""{0}""" & vbCrLf & vbCrLf & "{1}"
Dim formitem As String = String.Format(formdataTemplate, "params", "{""itemId"":" + unitID + ",""eventHash"":""jUploadForm1372773585167""}")
Dim formitembytes As Byte() = Encoding.UTF8.GetBytes(formitem)
memStream.Write(formitembytes, 0, formitembytes.Length)
Dim formitem2 As String = String.Format(formdataTemplate, "eventHash", "jUploadForm1372773585167")
Dim formitembytes2 As Byte() = Encoding.UTF8.GetBytes(formitem2)
memStream.Write(formitembytes2, 0, formitembytes2.Length)
Dim headerTemplate As String = "Content-Disposition: form-data; name=""{0}""; filename=""{1}""" & vbCrLf & "Content-Type: application/x-zip-compressed" & vbCrLf & vbCrLf
memStream.Write(boundarybytes, 0, boundarybytes.Length)
Dim header = String.Format(headerTemplate, "messages_filter_import_file", Path.GetFileName(filePath))
Dim headerbytes = Encoding.UTF8.GetBytes(header)
memStream.Write(headerbytes, 0, headerbytes.Length)
Using fileStream = New FileStream(filePath, FileMode.Open, FileAccess.Read)
Dim buffer = New Byte(1023) {}
Dim bytesRead As Integer = 0
While (fileStream.Read(buffer, 0, buffer.Length) <> 0)
memStream.Write(buffer, 0, bytesRead)
End While
End Using
memStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length)
s.ContentLength = memStream.Length
Dim requestStream As Stream = s.GetRequestStream()
memStream.Position = 0
Dim tempBuffer() As Byte = New Byte((memStream.Length) - 1) {}
memStream.Read(tempBuffer, 0, tempBuffer.Length)
memStream.Close()
requestStream.Write(tempBuffer, 0, tempBuffer.Length)
Dim Respuesta_JSON As String = ""
Dim response As HttpWebResponse = DirectCast(s.GetResponse(), HttpWebResponse)
Dim stream2 As Stream = response.GetResponseStream()
Dim reader2 As StreamReader = New StreamReader(stream2)
Respuesta_JSON = reader2.ReadToEnd()
I really appreciate your help!