Transfer images from server to local folder Windows Store App

落花浮王杯 提交于 2019-12-02 11:37:24

Big Thanks to ChicagoMike for putting me on the right track. Here is how I did it

Object to transfer from the service to the client :

Imports System.IO

    Public Class PictureSender
        Public Property PictureBytes As Byte()
        Public Property PictureName As String
    End Class

Code on the client calling its controller (The linq and For loop is useful in my case since I have multiple types of images in their respective folders):

    Private Async Sub btnGetPictures_Click(sender As Object, e As RoutedEventArgs)
        ' Get the list of all categories of picture to go through all folders from local saves.
        Dim lstFolders As List(Of String) = (From p In Await ImageController.GetImageDetails Select p.Categorie).Distinct.ToList()
        For i = 0 To lstFolders.Count - 1
            PicturesController.GetPicturesServer(lstFolders(i))
        Next
    End Sub

Code in the client controller :

    Public Async Sub GetPicturesServer(_folderName As String)
        Dim service As New ServiceReference1.Service1Client(ServiceReference1.Service1Client.EndpointConfiguration.BasicHttpBinding_IService1)

        Dim rcv = Await service.SelectAllPicturesAsync(_folderName)

        ' Get the folder
        Dim folder = ApplicationData.Current.LocalFolder
        folder = Await folder.CreateFolderAsync("Images", CreationCollisionOption.OpenIfExists)
        Await folder.CreateFolderAsync(_folderName, CreationCollisionOption.ReplaceExisting)
        folder = Await folder.GetFolderAsync(_folderName)

        ' Run through all pictures and save them
        For i = 0 To rcv.Count - 1
            Dim myfile As StorageFile = CType(Await folder.CreateFileAsync(rcv(i).PictureName, CreationCollisionOption.ReplaceExisting), StorageFile)
            Await Windows.Storage.FileIO.WriteBufferAsync(myfile, rcv(i).PictureBytes.AsBuffer())
        Next

Code on the service :

    Function Execute(TypeOfPictures As String) As List(Of PictureSender)
        Dim PicturesinDirectory As List(Of String) = Directory.GetFiles("\\SERVER\Data\Image\" + TypeOfPictures + "\", "*.jpg*").ToList()

        Dim lstPicturesToSend As List(Of PictureSender) = New List(Of PictureSender)
        For i = 0 To PicturesinDirectory.Count - 1
            Dim ByteArray As Byte() = File.ReadAllBytes(PicturesinDirectory(i))

            ' Affect the PictureSender object
            Dim PictureSenderObject As PictureSender = New PictureSender
            PictureSenderObject.PictureBytes = ByteArray
            PictureSenderObject.PictureName = PicturesinDirectory(i).Substring(PicturesinDirectory(i).LastIndexOf("\") + 1)
            lstPicturesToSend.Add(PictureSenderObject)
        Next
        Return lstPicturesToSend

    End Function

This code pretty much sends back a Byte() and a picture name from the service which is then used by the client. The client uses the buffer to write on the files and recreating the picture

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!