|
| 1 | +@page "/documentprocessing" |
| 2 | +@using System.Diagnostics |
| 3 | +@inject HttpUtil _httpUtil |
| 4 | +@inject IJSRuntime JsRuntime |
| 5 | + |
| 6 | +<h3>Document Processing</h3> |
| 7 | + |
| 8 | +<TelerikButton OnClick="@ExportPDF" Icon="file-pdf" Title="Export PDF"></TelerikButton> |
| 9 | +<TelerikButton OnClick="@MergePDF" Icon="file-ascx" Title="Merge PDF"></TelerikButton> |
| 10 | +<TelerikButton OnClick="@ExportDocx" Icon="file-word" Title="Export Docx"></TelerikButton> |
| 11 | +<TelerikButton OnClick="@ExportXlsx" Icon="file-excel" Title="Export Xlsx"></TelerikButton> |
| 12 | +<div class="k-form k-form-horizontal div-container"> |
| 13 | + <label>Zip Files Demo</label> |
| 14 | + <div class="k-form-field-wrap"> |
| 15 | + <TelerikUpload SaveUrl="@SaveUrl" |
| 16 | + AllowedExtensions="@AllowedExtensions" |
| 17 | + AutoUpload="false" |
| 18 | + MinFileSize="@MinFileSize" |
| 19 | + MaxFileSize="@MaxFileSize" |
| 20 | + Multiple="true" |
| 21 | + OnUpload="@UploadHandler"> |
| 22 | + </TelerikUpload> |
| 23 | + <div class="k-form-hint">Accepted files: <strong>DOCX, PDF, JPG and PNG</strong></div> |
| 24 | + <div calss="div-container"> |
| 25 | + <TelerikTextBox Width="200px" Value="@Password" Title="Password" ValueChanged="@PasswordChanged" PlaceHolder="Password for protect."></TelerikTextBox> |
| 26 | + <TelerikButton OnClick="@ZipFiles" Icon="file-zip" Title="Zip all uploaded files."></TelerikButton> |
| 27 | + </div> |
| 28 | + |
| 29 | + </div> |
| 30 | +</div> |
| 31 | +<div class="loader-container"> |
| 32 | + <TelerikLoader Class="loader-indicator" Type="@LoaderType.InfiniteSpinner" Size="@(ThemeConstants.Loader.Size.Large)" Visible="@IsLoad"></TelerikLoader> |
| 33 | +</div> |
| 34 | + |
| 35 | +@code { |
| 36 | + public bool IsLoad { get; set; } = false; |
| 37 | + public string Password { get; set; } |
| 38 | + public async Task PasswordChanged(string value) { |
| 39 | + Password = value; |
| 40 | + } |
| 41 | + public async Task ExportPDF() { |
| 42 | + IsLoad = true; |
| 43 | + var response = await _httpUtil.GetAsync("documentprocessing/exporttopdf"); |
| 44 | + var fileData = await response.Content.ReadAsByteArrayAsync(); |
| 45 | + Save(fileData, "application/pdf", "Sample Document.pdf"); |
| 46 | + } |
| 47 | + |
| 48 | + public async Task MergePDF() { |
| 49 | + IsLoad = true; |
| 50 | + var response = await _httpUtil.GetAsync("documentprocessing/mergepdf"); |
| 51 | + var fileData = await response.Content.ReadAsByteArrayAsync(); |
| 52 | + Save(fileData, "application/pdf", "Merge Sample Document.pdf"); |
| 53 | + } |
| 54 | + public async Task ExportDocx() { |
| 55 | + IsLoad = true; |
| 56 | + var response = await _httpUtil.GetAsync("documentprocessing/exporttodocx"); |
| 57 | + var fileData = await response.Content.ReadAsByteArrayAsync(); |
| 58 | + Save(fileData, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "Sample Document.docx"); |
| 59 | + } |
| 60 | + public async Task ExportXlsx() { |
| 61 | + IsLoad = true; |
| 62 | + var response = await _httpUtil.GetAsync("documentprocessing/exporttoxlsx"); |
| 63 | + var fileData = await response.Content.ReadAsByteArrayAsync(); |
| 64 | + Save(fileData, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Sample Document.xlsx"); |
| 65 | + |
| 66 | + } |
| 67 | + public string SaveUrl => "documentprocessing/uploadfiles"; |
| 68 | + public List<string> AllowedExtensions { get; set; } = new List<string>() { ".docx", ".pdf", ".jpg", ".png" }; |
| 69 | + public int MinFileSize { get; set; } = 1024; |
| 70 | + public int MaxFileSize { get; set; } = 4 * 1024 * 1024; |
| 71 | + public async Task UploadHandler(UploadEventArgs e) { |
| 72 | + e.RequestHeaders = new Dictionary<string, object>(); |
| 73 | + await _httpUtil.RefreshToken(e.RequestHeaders); |
| 74 | + } |
| 75 | + public async Task ZipFiles() { |
| 76 | + IsLoad = true; |
| 77 | + var response = await _httpUtil.GetAsync($"documentprocessing/zipfiles?password={Password}"); |
| 78 | + var fileData = await response.Content.ReadAsByteArrayAsync(); |
| 79 | + Save(fileData, "application/x-zip-compressed", "ZipDemo.zip"); |
| 80 | + } |
| 81 | + public void Save(byte[] byteData, string mimeType, string fileName) { |
| 82 | + if (byteData == null) { |
| 83 | + return; |
| 84 | + } |
| 85 | + var fileBase64=Convert.ToBase64String(byteData); |
| 86 | + JsRuntime.InvokeVoidAsync("saveFile", fileBase64, mimeType, fileName); |
| 87 | + IsLoad = false; |
| 88 | + } |
| 89 | +} |
0 commit comments