Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

浏览器不能正确响应下载操作 #5

Open
ghost opened this issue Jan 5, 2018 · 10 comments
Open

浏览器不能正确响应下载操作 #5

ghost opened this issue Jan 5, 2018 · 10 comments

Comments

@ghost
Copy link

ghost commented Jan 5, 2018

如果点击一个“可下载资源”的链接,浏览器不能下载相关资源。

@zhongwuzw
Copy link
Owner

下载的功能确实没有实现,可以考虑实现一下。

@ghost
Copy link
Author

ghost commented Jan 5, 2018

哈哈期待加入下载功能,同时还想请教一下实现的思路,想自己动手试试~。。~

@zhongwuzw
Copy link
Owner

思路就是,比如浏览器需要支持下载.zip文件,那么需要在 webView:shouldStartLoadWithRequest:navigationType:中判断navigationType类型,获取zipurl,拿到后就可以进行下载了,所以需要实现一个文件管理的界面,界面可以进行文件的管理,比如断点续传,删除等操作。

@ghost
Copy link
Author

ghost commented Jan 9, 2018

在webView:shouldStartLoadWithRequest:navigationType:中截取请求的链接,那我怎么知道那些链接是可下载的呢请问?

@zhongwuzw
Copy link
Owner

zhongwuzw commented Jan 9, 2018

这个问题就需要看需求了,首先,如果只需要初步实现下载功能的话,可以简单的通过后缀名,比如url后缀为.zip,那么我们可以判断他应该是一个zip文件,直接通过URLSession下载下来即可。
不过并不是每一个文件的地址都包含文件扩展名,如果需要覆盖所有的情况,有两种方法,一种是单独开一个入口,让用户输入URL,然后从响应中获取文件的名字,类型等等,HTTPheader 中有Content-DispositionContent-Type等可以获取相关信息。另一种是创建NSURLProtocol来管理所有的请求,拿到响应后的操作和第一种一样。

@ghost
Copy link
Author

ghost commented Jan 10, 2018

你的意思是不是:一个链接在未加载之前是无法得知其是不是“可下载”的。要先加载它,然后拦截它的响应,从响应的header中获取到文件的类型(比如.zip),从而判断它是不是“可下载”。我理解的对吗

@zhongwuzw
Copy link
Owner

是的。

@ghost
Copy link
Author

ghost commented Jan 10, 2018

好的,谢谢谢啦。

@zhongwuzw
Copy link
Owner

忘了,还有一个方法忘说,通过私有方法来判断,- (void)webView:(id)arg1 decidePolicyForMIMEType:(id)arg2 request:(id)arg3 frame:(id)arg4 decisionListener:(id)arg5;,该方法会检测到不同的Mimetype,在这个方法可以处理不同的MimeType.

@ghost
Copy link
Author

ghost commented Jan 12, 2018

👍 这份文档也提到,可以在这个方法处理下载,感谢啦~~
http://doc.mbalib.com/view/241f5e4aee6b5172fa947819d8c95a67.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant