private async void DownloadSongFromIdAsync(int id) { try { string json = ""; string httpRequestString = $"http://kaden.ghostsofutah.com:9578/music/getfile/{id}"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(httpRequestString); request.AutomaticDecompression = DecompressionMethods.GZip; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) using (Stream stream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(stream)) { json = reader.ReadToEnd(); } SongWithFileBytes songWithBytes = Newtonsoft.Json.JsonConvert.DeserializeObject <SongWithFileBytes>(json); string path = ""; path = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic) + "/Tempo/Downloads/"; (new FileInfo(path)).Directory.Create(); //path += get to users music folder //create tempo folder path += songWithBytes.song.title + ".mp3"; System.IO.File.WriteAllBytes(path, songWithBytes.fileBytes); } catch (Exception e) { MessageBox.Show("Error occured trying to download your song"); } }
private async void SubmitButton_Click(object sender, RoutedEventArgs e) { //cloudLibraryTable.Focus = Visibility.Hidden; //uploadGrid.Focus = Visibility.Visible; //this ^^^ includes all textbox fields and a submit and cancel button //First have user select mp3 file //Get the song detail strings from pop up window string filePath = filePathUploadTextBox.Text; if (filePath == "" || filePath == null || filePath.Split('.')[filePath.Split('.').Length - 1] != "mp3") { MessageBox.Show("That is an invalid File please choose an mp3 file before submitting"); } else { string title = titleUploadTextBox.Text; string artist = artistUploadTextBox.Text; string genre = genreUploadTextBox.Text; int hours = 0;// Make some kinda time entry thing for the file int minutes = 0; int seconds = 0; if (File.Exists(filePath)) { byte[] fileBytes = System.IO.File.ReadAllBytes(filePath); //get filesize from file int fileSize = fileBytes.Length / 1000; Song s = new Song(); s.title = title; s.artist = artist; s.genre = genre; s.hours = hours; s.minutes = minutes; s.seconds = seconds; s.fileSize = fileSize; SongWithFileBytes songWithFile = new SongWithFileBytes(s, fileBytes); string songWithFileJson = Newtonsoft.Json.JsonConvert.SerializeObject(songWithFile); //convert songWithFile to json //send songWithFile to: // http://kaden.ghostsofutah.com:9578/music/ // with a post request and the json in the body var httpClient = new HttpClient(); var response = await httpClient.PostAsync("http://kaden.ghostsofutah.com:9578/music/upload", new StringContent(songWithFileJson, System.Text.Encoding.UTF8, "application/json")); // "http://kaden.ghostsofutah.com:9578/music/upload" response.EnsureSuccessStatusCode(); CancelButton_Click(null, null); } else { MessageBox.Show("The specified File does not exist"); } } }