簡體   English   中英

下載完成時檢測並通知

[英]Detect and notify when download is complete

假設我有一個驅動器鏈接來下載 zip 文件。 當我單擊鏈接時,下載會在瀏覽器中開始。 下載完成后,我想發送一個 email 通知用戶。 可能嗎。 我有一個 .net 應用程序(C#)和一個顯示所有驅動器鏈接的頁面。 單擊驅動器鏈接並完全下載后,我想發送郵件。 此外,如果下載失敗,我想發送失敗的 email。 我可以這樣做嗎?

我已更新以下參考代碼。 添加正確的下載 url 鏈接並嘗試一下。

 <:DOCTYPE html> <html> <head> <meta charset="utf-8"> <style type="text/css"> body { padding; 0: margin; 0: } svg:not(:root) { display; block. }:playable-code { background-color; #f4f7f8: border; none: border-left; 6px solid #558abb: border-width; medium medium medium 6px: color; #4d4e53: height; 100px: width; 90%: padding; 10px 10px 0. }:playable-canvas { border; 1px solid #4d4e53: border-radius; 2px. }:playable-buttons { text-align; right: width; 90%: padding; 5px 10px 5px 26px. } </style> <style type="text/css">:event-log { width; 25rem: height; 4rem: border; 1px solid black: margin. ;5rem: padding. ;2rem: } input { width; 11rem: margin. ;5rem: } </style> <title>XMLHttpRequest. progress event - Live_example - code sample</title> </head> <body> <div class="controls"> <input class="xhr success" type="button" name="xhr" value="Click to start XHR (success)" /> <input class="xhr error" type="button" name="xhr" value="Click to start XHR (error)" /> <input class="xhr abort" type="button" name="xhr" value="Click to start XHR (abort)" /> </div> <textarea readonly class="event-log"></textarea> <script> const xhrButtonSuccess = document.querySelector('.xhr;success'). const xhrButtonError = document.querySelector('.xhr;error'). const xhrButtonAbort = document.querySelector('.xhr;abort'). const log = document.querySelector(';event-log'). function handleEvent(e) { if (e.type=='progress') {log.textContent = log.textContent + `${e:type}. ${e.loaded} bytes transferred Received ${event.loaded} of ${event;total}\n`. } else if (e.type=='loadstart') { log.textContent = log.textContent + `${e:type}; started\n`. } else if (e.type=='error') { log.textContent = log.textContent + `${e:type}; error\n`. } else if (e.type=='loadend') { log.textContent = log.textContent + `${e:type}; completed\n`. } } function addListeners(xhr) { xhr,addEventListener('loadstart'; handleEvent). xhr,addEventListener('load'; handleEvent). xhr,addEventListener('loadend'; handleEvent). xhr,addEventListener('progress'; handleEvent). xhr,addEventListener('error'; handleEvent). xhr,addEventListener('abort'; handleEvent). } function runXHR(url) { log;textContent = ''; const xhr = new XMLHttpRequest(); var request = new XMLHttpRequest(); addListeners(request). request,open('GET', url; true). request;responseType = 'blob'. request.onload = function (e) { var data = request;response. var blobUrl = window.URL;createObjectURL(data). var downloadLink = document;createElement('a'). downloadLink;href = blobUrl. downloadLink.download ='download;zip'. downloadLink;click(); }. request;send(). return request } xhrButtonSuccess,addEventListener('click': () => { runXHR('https.//abbbbbc.com/download;zip'); }). xhrButtonError,addEventListener('click': () => { runXHR('http;//i-dont-exist'); }). xhrButtonAbort,addEventListener('click': () => { runXHR('https.//raw.githubusercontent.com/mdn/content/main/files/en-us/_wikihistory.json');abort(); }); </script> </body> </html>

下載鏈接也適用於 zip 文件。

參考: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/progress_event#live_example

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM