测试了很长时间,使用了各种方法,后来在一开源下载工具FileSaver.js的讨论中了解到苹果这边目前暂不支持
相关讨论帖
- https://github.com/eligrey/FileSaver.js/issues/12
- https://github.com/eligrey/FileSaver.js/issues/375
- https://github.com/eligrey/FileSaver.js/issues/430
有用的讨论信息
No there is no alternative package if there where we would have fixed it already and the question have brought up many times.
So there is nothing we can do except to wait for ios to fix it
and since there is nothing to do i will close this.
But still, it does not show or popup the Download File dialog on IOS 11.4. I tried Safari, Chrome, and Firefox and all behaves in the same way. All the 3 browsers open the text file content in a new tab generating some weird filename which is visible on the tab title bar as well as in the URL bar.
Any idea of how can I popup a download file dialog to save the Text file in IOS? I am using AngularJS for my App Development.
So the conclusion here about safari is
- download attribute in safari is not supported
- It will try other means to save the blob by opening a new url
- If the mimetype can be rendered by safari it will be able to display it in a new tab
- If the mimetype is application/octet-stream:
4.1 Create a base64 link with FileReader api
4.2 try to open a new tab using window.open + base64 url
4.3 if it was more then 1 sec before the user interaction happened it will use the current page instead
but that is likely going to fail because (see first example using location.href) Failed to load resource: Frame load interrupted This may still work if the mimetype is not application/octet-stream and the saveAs was not called synchronous- Safari don’t have anything like msSaveAs()
- safest way to force the file to be saved is to have a data:attachment/file" + base64 ready and open that link using window.open() when the user interacts with the website (or at least to it under 1 second)
- when saving it as a attachment filename will be “unknown”
I suppose that the opening in new tab is expected behavior for Safari <12 on iOS because the browsers do not have an access to file system. This gives the user a chance to save a file with an additional click to the cloud storage.