Posts

Showing posts from November, 2023

Loading image/spinner for Anchor tag:

<a class="bg-grey-btn" data-docnum="ABC-1201" href="javascript:void(0);" onclick="downloadAFile(this); return false;">Download Document </a> Loading image/spinner for Anchor tag: Method-1: const downloadAFile = async (e) => { try { const btnElement = $(e); const btnVal = btnElement.html(); //To add css-based spinner to the button text. //if (btnElement.html().indexOf("spinner") < 0) btnElement.html(' ' + btnVal); btnElement.data('original-html', btnVal); //...... //...... //logic for service call and response handle. //...... //...... if (btnElement) { btnElement.html(btnElement.data('original-html')); } } catch (err) { console.error("Download failed:", err); if (btnElement) { btnElement.html(btnElement.data('original-html')); } } }; Method-2: const downloadAFile = async (e) => { try { const btnElement ...

MVC FileDownload as BLOB object using javascript

  MVC FileDownload as BLOB object using javascript Controller: ---------- public class BaseController : Controller { public async Task DownloadDocument(string id) { var tuple = default(Tuple ); try { var service = new xxxxxService(); //if (Regex.IsMatch(id, "^[a-zA-Z0-9-]+$"))//if (srdDocNum.All(char.IsLetterOrDigit)) if (id.Any(char.IsLetter)) tuple = await service.GetDownloadFileByDocNum(id); //SRD--Num else tuple = await service.DownloadDocumentAsync(id); if (tuple?.Item1 != null) { var bytes = (tuple.Item1).ToArray(); return File(bytes, "application/octet-stream", tuple.Item2); } else { //return View("~/Views/Error/PageNotFound.cshtml"); return File(new byte[0], "application/octet-stream", "nofile"); //Response.StatusCode = 404; } } catch (Exception ex) { LogHelper.Error($"Base/DownloadDocument: {ex.Message.ToString()}...