The original requirement is that there is an H5 page with a “click download” button. After clicking, the specific APK is downloaded.
Probably the following: (1)
When I received the request, I had a good time. The biggest advantage of this H5 page is that it can’t be used in micro-mail (micro-mail is a pit, various restrictions, it’s impossible to download APK directly from micro-mail browser) and it’s much simpler if it’s in normal browser.
So I came up with the first, change location. href when I click the download button.
// I found an APK download link as an example window.location.href = 'http://imtt.dd.qq.com/16891/26747DD8B125A8479AD0C9D22CA47BC9.apk?fsname=com.snda.wifilocating_4.2.91_3211.apk&csr=1bbd';
Take it in the browser and try it out. Many Android models can be accessed. Then Meizi delivered the task and packed up and went home.
The blank page in the screenshot is because I change the download link of href to apk. When the page opens, it will stay in a blank page, and then the progress bar of download APK will appear on the status bar of the mobile phone.
Since you don’t want a blank page, you can open it on the current page instead of opening it directly.
When I think of iframe, I can open a page on the original page with the help of iframe.
If you use iframe, you can either download or not reopen the page, and it will not have any impact on the layout of the original page. Finally, I adopt this scheme.
The third method is accidental learning. Since iframe can be used, it must be possible to use form. Form action can also send requests.
So I rewrote the second method:
var src = 'http://imtt.dd.qq.com/16891/26747DD8B125A8479AD0C9D22CA47BC9.apk?fsname=com.snda.wifilocating_4.2.91_3211.apk&csr=1bbd'; var form = document.createElement('form'); form.action = src; document.getElementsByTagName('body').appendChild(form); form.submit();
The above code can also fulfill the requirement of downloading apk.
In fact, the second and third methods are to expand the use of iframe and form. Looking at it separately, we know that iframe can nest sub-pages in the parent page, and that form action can request or jump pages. This is a very common function, but we do not know that they will be used to download files, and the effect is very good. It can be seen that knowledge still needs to be assimilated and integrated.
When I wrote this article, I checked iframe. When Ajax and other technologies did not appear, the usual method of long-term polling in the industry for some time was to use iframe. If you are interested, you can also study it.
The above is the whole content of this article. I hope it will be helpful to everyone’s study, and I hope you will support developpaer more.