Mobile Safari - Html download link opens tab inste

2019-08-13 14:35发布

问题:

I'm working with a webapp that has a link to download an xml file. As in, click the link, browser downloads the file. The file downloads as expected from Chrome/FF/Safari on my desktop. It also works from FF when I spoof my User Agent to be an iPad. But when clicked from Mobile Safari on an iPad, the link navigates the browser to a new page and tries to display the xml as html, without downloading anything.

Currently the HTTP Response contains the following headers:

Content-Disposition:attachment; filename="myFile.xml"    
Content-Type:application/octet-stream;charset=UTF-8

It's my understanding that these two HTTP headers should tell the browser to download, rather then display, the response stream.

Does someone know why this doesn't work in Mobile Safari, running on an iPad?

回答1:

MobileSafari will usually not download files, even if instructed to in an HTTP header. For example, it's impossible to download an MP3 from a website in MobileSafari, it's just going to play it. This stems from the iPhones/iPads not having a user-accessable file system - if you can't access the file system, there's really no way to save or access downloaded files. So, because MobileSafari can't download files, it's just going to try and display it, no matter what the HTTP header says.