這兩天碰到一個需求:在用戶刷新頁面或者關(guān)閉頁面的時候,前端要給后臺發(fā)一條請求,釋放該頁面的授權(quán)占用。
分析了一下,這不就是在頁面卸載時發(fā)請求嘛,三下五除二就實現(xiàn)一版:
window.addEventListener("beforeunload", ()=> {
let oReq=new XMLHttpRequest();
oReq.open("POST", "http://127.0.0.1:1991/loginout");
oReq.send(JSON.stringify({name: "編程三昧"}));
測試發(fā)現(xiàn):
既然異步 Ajax 不行,那就試試同步的吧,結(jié)果直接報錯了:
搜了一下,解釋如下:
Chrome now disallows synchronous XHR during page dismissal when the page is being navigated away from or closed by the user. This involves the following events (when fired on the path of page dismissal): beforeunload, unload, pagehide, and visibilitychange.
概括起來就是:對現(xiàn)在的 Chrome 來說,在頁面導(dǎo)航離開或者被用戶關(guān)閉時,不允許發(fā)送同步 XHR 請求,涉及到的事件有:beforeunload、unload、pagehide 和 visibilitychange。
雖然問題沒解決,但是卻長知識了,這波不太虧!
后來通過搜索,看到有一個接口是專門來干這事的,那就是 navigator.sendBeacon() 。
這個方法主要用于滿足統(tǒng)計和診斷代碼的需要,這些代碼通常嘗試在卸載(unload)文檔之前向web服務(wù)器發(fā)送數(shù)據(jù)。
navigator.sendBeacon(url, data);
當用戶代理成功把數(shù)據(jù)加入傳輸隊列時,sendBeacon() 方法將會返回 true,否則返回 false。
既然有了接口,那實現(xiàn)起來就簡單了。
window.addEventListener("beforeunload", (e)=> {
const data={name: "編程三昧"};
window.navigator.sendBeacon("http://127.0.0.1:1991/loginout", JSON.stringify(data));
});
不管是刷新頁面還是關(guān)閉頁面,后臺都能接收到前端發(fā)送過來的請求,完美實現(xiàn)需求。
瀏覽器現(xiàn)在功能越來越強大,支持的 API 也越來越豐富,放在之前很難實現(xiàn)的功能,現(xiàn)在可能就是輕而易舉的事,還是要多關(guān)注技術(shù)動態(tài)。
~
~本文完,感謝閱讀!
~
學(xué)習(xí)有趣的知識,結(jié)識有趣的朋友,塑造有趣的靈魂!
大家好,我是〖編程三昧〗的作者 隱逸王,我的公眾號是『編程三昧』,歡迎關(guān)注,希望大家多多指教!
你來,懷揣期望,我有墨香相迎! 你歸,無論得失,唯以余韻相贈!
知識與技能并重,內(nèi)力和外功兼修,理論和實踐兩手都要抓、兩手都要硬!
年(Light Year Admin)后臺管理系統(tǒng)模板是一個基于Bootstrap v3.3.7的純HTML模板。
作為后端開發(fā)人員,自己在做一些簡單系統(tǒng)時,經(jīng)常為了后臺的模板煩惱,國內(nèi)的少,也不太喜歡tab形式的;國外的又太復(fù)雜;vue什么框架的又不會用,因而想自己整理出來一個簡單點的通用后臺模板,結(jié)合自己的使用和國外模板的配色、細節(jié)處理,這就有了光年后臺模板。
簡潔而清新的后臺模板,功能雖少,倒也滿足簡單的后臺功能,也能夠快速上手,希望大家支持。
特別感謝
登錄頁面
后臺首頁
開關(guān)樣式
文檔列表
近有一個業(yè)務(wù)是前端要上傳word格式的文稿,然后用戶上傳完之后,可以用瀏覽器直接查看該文稿,并且可以在富文本框直接引用該文稿,所以上傳word文稿之后,后端保存到db的必須是html格式才行,所以涉及到word格式轉(zhuǎn)html格式。
通過調(diào)查,這個word和html的處理,有兩種方案,方案1是前端做這個轉(zhuǎn)換。方案2是把word文檔上傳給后臺,后臺轉(zhuǎn)換好之后再返回給前端。至于方案1,看到大家的反饋都說很多問題,所以就沒采用前端轉(zhuǎn)的方案,最終決定是后端轉(zhuǎn)化為html格式并返回給前段預(yù)覽,待客戶預(yù)覽的時候,確認格式?jīng)]問題之后,再把html保存到后臺(因為word涉及到的格式太多,比如圖片,visio圖,表格,圖片等等之類的復(fù)雜元素,轉(zhuǎn)html的時候,可能會很多格式問題,所以要有個預(yù)覽的過程)。
對于word中普通的文字,問題倒不大,主要是文本之外的元素的處理,比如圖片,視頻,表格等。針對我本次的文章,只處理了圖片,處理的方式是:后臺從word中找出圖片(當然引入的jar包已經(jīng)帶了獲取word中圖片的功能),上傳到服務(wù)器,拿到絕對路徑之后,放入到html里面,這樣,返回給前端的html內(nèi)容,就可以直接預(yù)覽了。
maven引入相關(guān)依賴包如下:
<poi-scratchpad.version>3.14</poi-scratchpad.version>
<poi-ooxml.version>3.14</poi-ooxml.version>
<xdocreport.version>1.0.6</xdocreport.version>
<poi-ooxml-schemas.version>3.14</poi-ooxml-schemas.version>
<ooxml-schemas.version>1.3</ooxml-schemas.version>
<jsoup.version>1.11.3</jsoup.version>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>${poi-scratchpad.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi-ooxml.version}</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>xdocreport</artifactId>
<version>${xdocreport.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>${poi-ooxml-schemas.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>${ooxml-schemas.version}</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>${jsoup.version}</version>
</dependency>
word轉(zhuǎn)html,對于word2003和word2007轉(zhuǎn)換方式不一樣,因為word2003和word2007的格式不一樣,工具類如下:
使用方法如下:
public String uploadSourceNews(MultipartFile file) {
String fileName=file.getOriginalFilename();
String suffixName=fileName.substring(fileName.lastIndexOf("."));
if (!".doc".equals(suffixName) && !".docx".equals(suffixName)) {
throw new UploadFileFormatException();
}
DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyyMM");
String dateDir=formatter.format(LocalDate.now());
String directory=imageDir + "/" + dateDir + "/";
String content=null;
try {
InputStream inputStream=file.getInputStream();
if ("doc".equals(suffixName)) {
content=wordToHtmlUtil.Word2003ToHtml(inputStream, imageBucket, directory, Constants.HTTPS_PREFIX + imageVisitHost);
} else {
content=wordToHtmlUtil.Word2007ToHtml(inputStream, imageBucket, directory, Constants.HTTPS_PREFIX + imageVisitHost);
}
} catch (Exception ex) {
logger.error("word to html exception, detail:", ex);
return null;
}
return content;
}
關(guān)于doc和docx的一些存儲格式介紹:
docx 是微軟開發(fā)的基于 xml 的文字處理文件。docx 文件與 doc 文件不同, 因為 docx 文件將數(shù)據(jù)存儲在單獨的壓縮文件和文件夾中。早期版本的 microsoft office (早于 office 2007) 不支持 docx 文件, 因為 docx 是基于 xml 的, 早期版本將 doc 文件另存為單個二進制文件。
DOCX is an XML based word processing file developed by Microsoft. DOCX files are different than DOC files as DOCX files store data in separate compressed files and folders. Earlier versions of Microsoft Office (earlier than Office 2007) do not support DOCX files because DOCX is XML based where the earlier versions save DOC file as a single binary file.
可能你會問了,明明是docx結(jié)尾的文檔,怎么成了xml格式了?
很簡單:你隨便選擇一個docx文件,右鍵使用壓縮工具打開,就能得到一個這樣的目錄結(jié)構(gòu):
所以你以為docx是一個完整的文檔,其實它只是一個壓縮文件。
參考:
https://www.cnblogs.com/ct-csu/p/8178932.html
*請認真填寫需求信息,我們會在24小時內(nèi)與您取得聯(lián)系。