Monday, 25 January 2016

coding for window 10 file download from server

string _localhost = "http://localhost:XXXXX/";
string _fileType= "video/mp4";

 public async void Filedownload(IList<TableContract> tableList)
        {
            string _pathforImage = Windows.Storage.ApplicationData.Current.LocalFolder.Path + "\\Image";
            string _pathforVideo= Windows.Storage.ApplicationData.Current.LocalFolder.Path + "\\Video";
            string _folderPath;
            FolderCreation(_pathforImage);
            FolderCreation(_pathforVideo);

            foreach (var NewtableList in tableList)
            {
                _folderPath = NewtableList.tableType.Trim() == _fileType ? _pathforVideo : _pathforImage;
                string FilePath = NewtableList.ContentPath;
                using (var httpClient = new HttpClient())
                {
                    using (var request = new HttpRequestMessage(HttpMethod.Get, _localhost + FilePath.Remove(0, 2)))
                    {
                        using (
                            Stream contentStream = await (await httpClient.SendAsync(request)).Content.ReadAsStreamAsync(),
                          stream = new FileStream(_folderPath + "\\"+ NewtableList.FileName, FileMode.Create, FileAccess.Write, FileShare.None))
                        {
                            await contentStream.CopyToAsync(stream);
                        }
                    }
                }
            }


           

        }

        public void FolderCreation(string Path)
        {
            if (!Directory.Exists(Path))
            {
                Directory.CreateDirectory(Path);
            }
        }

No comments:

Post a Comment