Skip to content

Commit

Permalink
Added ability to do an export as CSV from the repeater list menu. Tec…
Browse files Browse the repository at this point in the history
…hnically this addresses #1, but it's working.
  • Loading branch information
jsb2092 committed Oct 11, 2021
1 parent 73cb806 commit 15cdf2a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 459 deletions.
19 changes: 17 additions & 2 deletions RepeaterQTH/Pages/GetRepeaters.razor
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
@using MongoDB.Bson.Serialization.Options


<h1>Repeater Directory</h1>
<div style="float: left"><h1>Repeater Directory</h1></div>
<div style="float: right; margin-top: 20px"> <button @onclick="@DownloadText">Export</button></div>


@if (repeaters == null)
Expand All @@ -26,9 +27,10 @@
else
{
<RepeaterList repeaters=repeaters/>

}


@code {
[Parameter]
#nullable enable
Expand Down Expand Up @@ -86,5 +88,18 @@ else

}

async Task DownloadText()
{
string data = "Callsign, RxFreq, TxFreq, Tone, Tone Type, State, Location\n";
foreach (var repeater in repeaters)
{
data += repeater.CallSign + ", " + repeater.RxFreq + ", " + repeater.TxFreq + ", " +repeater.CTCSS+"," + repeater.Tone + ", " + repeater.State + ", "+repeater.LocationInfo + "\n";
}

byte[] file = System.Text.Encoding.UTF8.GetBytes(data);
var fileName = "rqth" + DateTime.Now.ToString("yyyyMMddHHmm")+".csv";
await jsRuntime.InvokeVoidAsync("BlazorDownloadFile", fileName, "text/csv", file);
}

}

1 change: 1 addition & 0 deletions RepeaterQTH/Pages/_Host.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<!--<script src="js/leafletBlazorInterops.js"></script>-->
<script src="_content/BlazorLeaflet/js/leafletBlazorInterops.js"></script>
<script src="_content/BrowserInterop/scripts.js"></script>
<script src="js/download.js"></script>
<script>
window.getIpAddress = () => {
return fetch('https://jsonip.com/')
Expand Down
46 changes: 46 additions & 0 deletions RepeaterQTH/wwwroot/js/download.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
function BlazorDownloadFile(filename, contentType, content) {
// Blazor marshall byte[] to a base64 string, so we first need to convert the string (content) to a Uint8Array to create the File
const data = base64DecToArr(content);

// Create the URL
const file = new File([data], filename, { type: contentType });
const exportUrl = URL.createObjectURL(file);

// Create the <a> element and click on it
const a = document.createElement("a");
document.body.appendChild(a);
a.href = exportUrl;
a.download = filename;
a.target = "_self";
a.click();

// We don't need to keep the url, let's release the memory
// On Safari it seems you need to comment this line... (please let me know if you know why)
URL.revokeObjectURL(exportUrl);
}

// Convert a base64 string to a Uint8Array. This is needed to create a blob object from the base64 string.
// The code comes from: https://developer.mozilla.org/fr/docs/Web/API/WindowBase64/D%C3%A9coder_encoder_en_base64
function b64ToUint6(nChr) {
return nChr > 64 && nChr < 91 ? nChr - 65 : nChr > 96 && nChr < 123 ? nChr - 71 : nChr > 47 && nChr < 58 ? nChr + 4 : nChr === 43 ? 62 : nChr === 47 ? 63 : 0;
}

function base64DecToArr(sBase64, nBlocksSize) {
var
sB64Enc = sBase64.replace(/[^A-Za-z0-9\+\/]/g, ""),
nInLen = sB64Enc.length,
nOutLen = nBlocksSize ? Math.ceil((nInLen * 3 + 1 >> 2) / nBlocksSize) * nBlocksSize : nInLen * 3 + 1 >> 2,
taBytes = new Uint8Array(nOutLen);

for (var nMod3, nMod4, nUint24 = 0, nOutIdx = 0, nInIdx = 0; nInIdx < nInLen; nInIdx++) {
nMod4 = nInIdx & 3;
nUint24 |= b64ToUint6(sB64Enc.charCodeAt(nInIdx)) << 18 - 6 * nMod4;
if (nMod4 === 3 || nInLen - nInIdx === 1) {
for (nMod3 = 0; nMod3 < 3 && nOutIdx < nOutLen; nMod3++, nOutIdx++) {
taBytes[nOutIdx] = nUint24 >>> (16 >>> nMod3 & 24) & 255;
}
nUint24 = 0;
}
}
return taBytes;
}
Loading

0 comments on commit 15cdf2a

Please sign in to comment.