Private Sub UploadOperation(ByVal operation As String)
Dim chunkSize As Long = 50 * 1024 '50KB
Dim fileInfo As FileInfo = Nothing
Dim fileLength As Integer = 0
'*** Call Binary Service ***
For Each FilePath As String In OpenFileDialog1.FileNames
Try
'Get upload attachment information
fileInfo = New FileInfo(FilePath)
Dim fileSizeInBytes As Long = fileInfo.Length
Using iStream As New FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read)
'Call binary service to create a message header
binaryData = New EntityBinary.BinaryData
binaryData = binaryClient.Create(binaryData)
'Get total size for last packet
Dim lastPacketDataSize As Long = fileSizeInBytes Mod chunkSize
'Total packet
Dim totalChunkPackets As Integer = Int((fileSizeInBytes / chunkSize)) + IIf(lastPacketDataSize > 0, 1, 0)
Dim completePacketData() As Byte = Nothing
completePacketData = New Byte(chunkSize - 1) {}
Dim countPacket As Integer = 0
Dim chunkSequenceNumber As Integer
Dim chunkPacketToWrite() As Byte
Dim dataToRead As Long = iStream.Length
While dataToRead > 0
If countPacket = totalChunkPackets - 1 Then
'Special handle for last packet of data due to the data size could less than 50 KB
Dim lastPacketData() As Byte = Nothing
lastPacketData = New Byte(lastPacketDataSize - 1) {}
fileLength = iStream.Read(lastPacketData, 0, lastPacketDataSize)
chunkPacketToWrite = lastPacketData
Else
'Read chunk data and assign as [complete packet]
fileLength = iStream.Read(completePacketData, 0, chunkSize)
chunkPacketToWrite = completePacketData
End If
'Upload chunk
With binaryData
.Sequence = New EntityBinary.EntityInteger With {.Value = countPacket, .IsDirty = True}
.Data = New EntityBinary.EntityObject With {.Value = chunkPacketToWrite, .IsDirty = True}
End With
binaryData = binaryClient.Save(binaryData)
chunkSequenceNumber = binaryData.Sequence.Value
If Not String.IsNullOrEmpty(chunkSequenceNumber) Then
dataToRead = dataToRead - fileLength
'Count total packet
countPacket += 1
Else
'Count total packet
countPacket -= 1
End If
End While
End Using
Catch ex As Exception
MessageBox.Show("Error Occurred :" & ex.Message, "Entity Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Next
'*** Call Document Attachment Service ***
Try
Dim docAttachmentData As New EntityDocumentAttachment.DocumentAttachmentData
With docAttachmentData
If Len(txtDocumentAttachmentID.Text) > 0 Then
.ID = New EntityDocumentAttachment.EntityGuid With {.Value = New Guid(txtDocumentAttachmentID.Text), .IsDirty = True}
End If
.AttachmentFileName = New EntityDocumentAttachment.EntityString With {.Value = Path.GetFileName(fileInfo.FullName), .IsDirty = True}
.AttachmentFileExtension = New EntityDocumentAttachment.EntityString With {.Value = fileInfo.Extension, .IsDirty = True}
.DocumentID = New EntityDocumentAttachment.EntityGuid With {.Value = New Guid(txtDocID.Text), .IsDirty = True}
.MessageID = New EntityDocumentAttachment.EntityGuid With {.Value = binaryData.MessageID.Value, .IsDirty = True}
End With
Select Case operation.ToLower
Case "create"
docAttachmentData = docAttachmentClient.Create(docAttachmentData)
MessageBox.Show("Document Attachment created successfully")
Case "save"
docAttachmentData = docAttachmentClient.Save(docAttachmentData)
MessageBox.Show("Document Attachment saved successfully")
Case "update"
docAttachmentClient.Update(docAttachmentData)
MessageBox.Show("Document Attachment updated successfully")
End Select
'Assign document attachment id return from service
If Not docAttachmentData.ID.Value.Equals(System.Guid.Empty) Then
txtDocumentAttachmentID.Text = docAttachmentData.ID.Value.ToString
End If
'Assign value return from service to respective text box in document attachment information section
txtAttachmentFileName.Text = docAttachmentData.AttachmentFileName.Value
txtAttachmentFileExtension.Text = docAttachmentData.AttachmentFileExtension.Value
txtDocID2.Text = docAttachmentData.DocumentID.Value.ToString
txtVersionNumber.Text = docAttachmentData.VersionNumber.Value
Catch ex As Exception
MessageBox.Show("Error Occurred :" & ex.Message, "Entity Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try
|