async Task DownloadAsync() { byte[] file = Array.Empty <byte>(); // Generate a text file if (ToDownload is not null) { file = System.Text.Encoding.UTF8.GetBytes(JsonSerializer.Serialize <T>(ToDownload)); } if (BlazorDownloadFileService is not null) { await BlazorDownloadFileService.DownloadFileAsync(Filename, file); } }
public void ExportBrief(IEnumerable <Card> deck) { Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Table table = builder.StartTable(); foreach (Card c in deck) { builder.InsertCell(); if (c.Question == null) { builder.Write(""); } else { builder.Write(c.Question); } // Build the second cell builder.InsertCell(); if (c.Answer == null) { builder.Write(""); } else { builder.Write(c.Answer); } // Call the following method to end the row and start a new row. builder.EndRow(); } table.SetBorders(LineStyle.None, 0, Color.White);; // Signal that we have finished building the table. builder.EndTable(); MemoryStream dstStream = new MemoryStream(); doc.Save(dstStream, SaveFormat.Docx); BlazorDownloadFileService.DownloadFile("out.docx", dstStream, "application/octet-stream"); }
public async Task OnClickDownloadViaHttpClientButton() { // Please imagine the situation that the downloading files API is protected by // token-based authorization (not cookie-based authorization). // In that case, the user can not download it from the href link of the anchor tag directly. // In this scenario, the application has to get a byte array of the file from // the API endpoint by HttpClient with token and make the byte array can be downloadable. var fileName = "/Users/themakers/Documents/PREDICAS/archivo1.PDF";//"/Users/themakers/Documents/PREDICAS/Nivel Biblico 1.pdf"; using var call = FileClient.DownloadStream(new FileRequest { Path = fileName }); // var bytes = await HttpClient.GetByteArrayAsync("pictures/1"); try { var result = new StringBuilder(); Console.WriteLine("Descargando archivo."); await foreach (var message in call.ResponseStream.ReadAllAsync()) { result.Append(Encoding.UTF8.GetString(message.Content.ToByteArray())); } // byte[] file = Encoding.ASCII.GetBytes(result.ToString()); Console.WriteLine("iniciando descarga local archivo."); // int bufferSize =65536;// 32768; new MemoryStream(Convert.FromBase64String(Base64Nyan)), "application/octet-stream"))>Dowload Nyan From Stream</button> // string contentType = "application/octet-stream"; await BlazorDownloadFileService.DownloadFile("nombrearchivo.pdf", new MemoryStream(Convert.FromBase64String(result.ToString())), "application/pdf"); // await JSRuntime.SaveAs( fileName, Convert.FromBase64String(result.ToString())); Console.WriteLine("termina descarga "); } catch (Exception ex) { Console.WriteLine($"DownloadFile Exception => Message {ex.Message} - InnerExceptionMessage {(ex.InnerException != null ? ex.InnerException.Message : "No InnerException")}"); } }
private async void btnDownload_Click() { var file = SelectedItem as FileInfo; if (file == null) { return; } if (DownloadFileAction != null) { DownloadFileAction.Invoke(JSRuntime, file.FullName); return; } System.Threading.CancellationTokenSource cts = new System.Threading.CancellationTokenSource(); loading.Show(TextDownload, file.Name, false, cts.Cancel); var stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); DateTime lastDisplayTime = DateTime.MinValue; byte[] buffer = new byte[24 * 1024]; var fileSize = file.Length; long readTotalCount = 0; try { using (var fs = file.OpenRead()) { while (!cts.IsCancellationRequested) { var ret = fs.Read(buffer, 0, buffer.Length); if (ret <= 0) { break; } readTotalCount += ret; if ((DateTime.Now - lastDisplayTime).TotalSeconds > 0.5 && stopwatch.ElapsedMilliseconds > 0) { StringBuilder sb = new StringBuilder(); var speed = Convert.ToDouble(readTotalCount / stopwatch.ElapsedMilliseconds); sb.Append(TextTransferSpeed + ": " + storageUSC.GetString(Convert.ToDecimal(speed * 1000), 1, true) + "B/s"); var remainingTime = TimeSpan.FromMilliseconds((fileSize - readTotalCount) / speed); sb.Append("," + TextRemainingTime + ": " + remainingTime.ToString(@"hh\:mm\:ss")); loading.UpdateProgress(Convert.ToInt32(readTotalCount * 100 / fileSize), sb.ToString()); await InvokeAsync(StateHasChanged); lastDisplayTime = DateTime.Now; } await BlazorDownloadFileService.AddBuffer(new ArraySegment <byte>(buffer, 0, ret), cts.Token); } } if (cts.IsCancellationRequested) { alert.Show(TextUpload, TextCanceled); return; } var result = await BlazorDownloadFileService.DownloadBinaryBuffers(file.Name, cts.Token); if (!result.Succeeded) { alert.Show(TextDownload, TextFailed + Environment.NewLine + result.ErrorName + Environment.NewLine + result.ErrorMessage); } } catch (TaskCanceledException) { alert.Show(TextUpload, TextCanceled); } catch (Exception ex) { alert.Show(TextUpload, TextFailed + Environment.NewLine + ExceptionUtils.GetExceptionMessage(ex)); } finally { await BlazorDownloadFileService.ClearBuffers(); stopwatch.Stop(); loading.Close(); } }
async Task DownloadFileAsync() { if (CheckedAudioOnlyStreamInfoHashCode == NoSelection) { return; } Progress = 0; DownloadButtonEnabled = false; try { var streamInfo = AudioOnlyStreamInfos.Single(x => x.GetHashCode() == CheckedAudioOnlyStreamInfoHashCode); var x = new Stopwatch(); x.Reset(); x.Start(); // bool oggOpusIsExtractedByServer = false; Stream stream; if (streamInfo.IsOpus() && ExtractOpus) { stream = await YouTubeDownloadApi.GetOggOpusAudioStreamAsync(streamInfo); // oggOpusIsExtractedByServer = true; x.Stop(); Console.WriteLine("GetOggOpusAudioStreamAsync = " + x.Elapsed); } else { stream = await YouTubeDownloadApi.GetAudioStreamAsync(streamInfo); x.Stop(); Console.WriteLine("GetAudioStreamAsync = " + x.Elapsed); } var filename = GetFileNameWithExtension(streamInfo); x.Reset(); x.Start(); using var memoryStream = new MemoryStream(); long lastValue = 0; await stream.CopyToAsync(memoryStream, async (e) => { Progress = 100 * e.TotalBytesRead / e.SourceLength; if (Progress != lastValue) { lastValue = Progress; StateHasChanged(); await Task.Delay(5); // give the UI some time to catch up } }); x.Stop(); Console.WriteLine("CopyToAsync = " + x.Elapsed); //byte[] array; //if (OpusAudioStreamPresent && ExtractOpus && !oggOpusIsExtractedByServer) //{ // using var opusStream = new MemoryStream(); // x.Start(); // memoryStream.Position = 0; // MatroskaDemuxer.ExtractOggOpusAudio(memoryStream, opusStream); // Console.WriteLine("ExtractOggOpusAudio and ToArray = " + x.Elapsed); // array = opusStream.ToArray(); // x.Stop(); //} //else //{ // x.Start(); // array = memoryStream.ToArray(); // x.Stop(); // Console.WriteLine("ToArray = " + x.Elapsed); //} x.Reset(); x.Start(); byte[] array = memoryStream.ToArray(); x.Stop(); Console.WriteLine("ToArray = " + x.Elapsed); x.Reset(); x.Start(); await BlazorDownloadFileService.DownloadFileAsync(filename, array); x.Stop(); Console.WriteLine("Download = " + x.Elapsed); } finally { DownloadButtonEnabled = true; } }
protected async Task GenerarClaves() { Byte[] ficheroZip = ServicioSeguridad.GenerarClavesRSA(); await BlazorDownloadFileService.DownloadFile("claves_rsa.zip", Convert.ToBase64String(ficheroZip)); }