[TIMOB-23309] Android: pdf and xls files with response headers (Content-Type →application/octet-stream) not opening with Ti sdk 5.0.0,5.1.1, 5.2.0
GitHub Issue | n/a |
---|---|
Type | Bug |
Priority | Critical |
Status | Closed |
Resolution | Fixed |
Resolution Date | 2016-07-11T06:48:45.000+0000 |
Affected Version/s | Release 5.4.0, Release 5.3.0 |
Fix Version/s | Release 6.0.0 |
Components | Android |
Labels | Android |
Reporter | Parween Singh |
Assignee | Ashraf Abu |
Created | 2016-04-26T10:20:46.000+0000 |
Updated | 2016-08-16T22:05:35.000+0000 |
Description
Steps to Reproduce
Download the Sample Pdf Viewer Code from 'https://gist.github.com/lbrenman/fa5195aba08d1b5e8fa0' Replace the url in line no 66, index.js file with 'http://dev-cms-molpower.trafficmanager.net/ServiceProfileFileHandler.ashx?svc=CMI&file=FirstSchedule.pdf' for pdf. On clicking the URL button . We get an alert 'This document cannot be opened'. The same works with Ti SDK 4.0.0.Actual Result
Pdf / Xls not getting openedExpected Result
The pdf should be opened successfullyAttachments
File | Date | Size |
---|---|---|
.log | 2016-04-26T10:20:50.000+0000 | 0 |
diagnostic5508536153902136859.log | 2016-04-26T10:22:11.000+0000 | 117068 |
Hello, I am able to reproduce this issue. It seems that when build with SDK 5.2.2.GA downloaded pdf file becomes corrupted. *Setps:* 1. Create a default alloy project. 2. Replace corresponding file code with following. *index.js :*
*index.tss :*
*index.xml :*
4. Build and run the app with SDK 4.0.0.GA. 5. Click
URL
button. >> PDF will be downloaded >> PDF will be shown.[Screenshot](https://i.imgsafe.org/6692c1b.png) 6. Now build and run the app with SDK 5.2.2.GA. 7. ClickURL
button >> >> PDF will be shown. Note: PDF is already download with previous steps and does exists in storage. 9. Delete the PDF from storage manually. 10. Run the app again and ClickURL
button. PDF will be downloaded (or may be not but written in storage anyway). >> "Document cannot be opened" alert shown.[Screenshot](https://i.imgsafe.org/b4eaa76.png) Note: No error log is produced 12. Build and run the app with SDK 4.0.0.GA again. 13. ClickURL
button. >> "Document cannot be opened" alert shown. 14. Delete the PDF from storage manually. 15. ClickURL
button. >> PDF will be downloaded >> PDF will be shown. *Environment*: *Device info:* HTC One (Android 4.4.3) *Node.js Version:* 0.12.7 *npm Version:* 2.11.3 *Titanium SDKs:* 5.2.2.GA and 5.2.1.GA *Java Development Kit Version:* 1.8.0_73 *Titanium CLI Version:* 5.0.5 *Appcelerator CLI Version:* 5.2.2 *Appcelerator Studio:* 4.5.0[~nsalahin] Can you also reproduce using sdk < 5.0.0 to see if it's a valid regression.
[~nsalahin] Can you include the PDF file that you tested for consistency purposes? Thanks,
When you add this into the test code, you will get a Status 302. "Object moved" with the URL 'http://dev-cms-molpower.trafficmanager.net/ServiceProfileFileHandler.ashx?svc=CMI&file=FirstSchedule.pdf'. When you use the relocated url "https://molprdeastus.blob.core.windows.net/molpowercmsprodcontent/Content/ServicesNSchedules/Network/CMI/FirstSchedule.pdf" , you can download it.
This is due to the redirect from http to https.
There is a change in behaviour as we are now using HttpURLConnection in the SDK from SDK 5 onwards. This document explains the issue. Android docs https://developer.android.com/reference/java/net/HttpURLConnection.html :- {quote} Response Handling HttpURLConnection will follow up to five HTTP redirects. It will follow redirects from one origin server to another. This implementation doesn't follow redirects from HTTPS to HTTP or vice versa. {quote} The problem in this case is that the original url is "HTTP" and the relocated/redirected url is "HTTPS". That is why it doesn't follow through. This is how Android is behaving.
Thus, to get this working, you should get the new relocated url and open another connection. This will download the file. Example:-
-Resolving issue as solution is provided above.-
Code above is a workaround for now while a fix is in progress.
PR for review: https://github.com/appcelerator/titanium_mobile/pull/8043
Tested the workaround provided by Ashraf Abu with Ti sdk 5.2.0 . It works as expected. Thanks.
Welcome. There is a fix in the coming up 6.0.0 that would not require this workaround.
PR Merged.
Verified the fix. Closing. Environment: Appc Studio : 4.7.0.201607250649 Ti SDK : 6.0.0.v20160816021339 Ti CLI : 5.0.9 Alloy : 1.9.1 MAC El Capitan : 10.11.6 Appc NPM : 4.2.7 Appc CLI : 6.0.0-24 Node: 4.4.4 Nexus 6 - Android 6.0.1