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

Unable to download correct .zip file on this website: https://resident.uidai.gov.in/offline-kyc #254

Open
SagarGiri2104 opened this issue Sep 27, 2019 · 29 comments
Labels

Comments

@SagarGiri2104
Copy link

I am using advance Webview to download .zip file in my app from UIDAI website. But when DownloadListener is getting called It returns the correct data, but downloads it's HTML webpage in that .zip file instead of downloading the actual file. whereas when I try downloading the same zip via chrome. It is downloading the correct zip file. Please help me with this problem. why is this happening in webview on this particular website (https://resident.uidai.gov.in/offline-kyc) whereas with the use of same onDownloadRequested function I am able to download files on other websites?

My code:

public class MainActivity 
   extends AppCompatActivity implements AdvancedWebView.Listener {
     private static final String QT_TAG = "QT_TAG";
     private AdvancedWebView mWebView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mWebView = (AdvancedWebView) findViewById(R.id.webview);
        mWebView.setListener(this, this);
        String url = "https://resident.uidai.gov.in/offline-kyc";
        mWebView.loadUrl(url);
    }

    @Override
    public void onDownloadRequested(String url, String suggestedFilename, String mimeType,
                                    long contentLength, String contentDisposition, String userAgent) {
        Log.i(QT_TAG, "downloadlistener0: "
                + userAgent +" " + mimeType+" " +  contentLength + " "
                + contentDisposition+ " " + url +"  "+suggestedFilename);

        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

        request.setMimeType(mimeType);
        request.setDestinationUri(Uri.fromFile(new File( Environment.DIRECTORY_DOWNLOADS)));
        //------------------------COOKIE!!------------------------
        String cookies = CookieManager.getInstance().getCookie(url);
        request.addRequestHeader("cookie", cookies);
        //------------------------COOKIE!!------------------------
        request.addRequestHeader("User-Agent", userAgent);
        request.setDescription("Downloading file...");
        request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimeType));
        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimeType));
        DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        dm.enqueue(request);
        Toast.makeText(getApplicationContext(), "Downloading File0 ", Toast.LENGTH_LONG).show();
    }

    @SuppressLint("NewApi")
    @Override
    protected void onResume() {
        super.onResume();
        mWebView.onResume();
        Log.i(QT_TAG, "onResume: ");
        // ...
    }

    @SuppressLint("NewApi")
    @Override
    protected void onPause() {
        mWebView.onPause();
        Log.i(QT_TAG, "onPause: ");
        // ...
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        mWebView.onDestroy();
        Log.i(QT_TAG, "onDestroy: ");
        // ...
        super.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (intent.getData() != null){
            Log.i(QT_TAG, "onActivityResult: "+ requestCode+" "+ resultCode+" "+intent.getData().toString());
        }else{
            Log.i(QT_TAG, "onActivityResult: "+ requestCode+" "+ resultCode);
        }

        mWebView.onActivityResult(requestCode, resultCode, intent);
        // ...
    }

    @Override
    public void onBackPressed() {
        if (!mWebView.onBackPressed()) {
            Log.i(QT_TAG, "onBackPressed: ");
            return; }
        // ...
        super.onBackPressed();
    }


    @Override
    public void onPageStarted(String url, Bitmap favicon) {
        Log.i(QT_TAG, "onPageStarted: "+ url);
    }

    @Override
    public void onPageFinished(String url) {
        Log.i(QT_TAG, "onPageFinished: "+ url);
    }

    @Override
    public void onPageError(int errorCode, String description, String failingUrl) {
        Log.i(QT_TAG, "onPageError "+ errorCode + " "+ description  +" "+failingUrl);
    }

    @Override
    public void onExternalPageRequest(String url) {
        Log.i(QT_TAG, "onExternalPageRequest: "+ url);

    }
}
`
@ashish986
Copy link

Hi buddy,

i am facing same problem as it is. i didn't found any solutions on google. I am able to download some tricky codes but and got xml data . but i want to get same file from webview.

@gouravbansal
Copy link

@ ashish986 please help me out with tricky code which you used to get xml data.

@rajeevmnnit
Copy link

@ ashish986 Help me out with tricky code which you used to download XML data from https://resident.uidai.gov.in/offline-kyc

@ashish986
Copy link

ashish986 commented Feb 7, 2020 via email

@ashish986
Copy link

ashish986 commented Feb 7, 2020 via email

@gouravbansal
Copy link

@ashish986 is this working on devices without chrome..

@ashish986
Copy link

ashish986 commented Feb 25, 2020 via email

@gouravbansal
Copy link

hi
1, what you means by normal web browser..
2, how you get information of file completely downloaded..

@ashish986
Copy link

ashish986 commented Feb 25, 2020 via email

@gouravbansal
Copy link

@ashish986
Hi
Not able to download using chrome tab.. can you share sample code

@ashish986
Copy link

ashish986 commented Feb 25, 2020 via email

@nurul12121
Copy link

@ashish986 Hi Ashish. I am trying to implement the same in android and facing the same issue. 1) Is there no other way to do the same in WebView?
2) If using CustomTabsIntent, how to get callback once zip file is downloaded in CustomTabsIntent?
Thanks.

@suvansh19
Copy link

@nurul12121 Any workaround you found for this?

@ashutoshnagar
Copy link

@nurul12121 @suvansh19 Did you guy find any work around this?

@ashish986
Copy link

ashish986 commented Jul 19, 2020 via email

@ashutoshnagar
Copy link

ashutoshnagar commented Jul 19, 2020

@ashish986 Actually, corrupt file is getting downloaded through webview, so wanted to know if anyone found solution, if not then how to use custom tabs, never used it..can you pls help

And if you can share code that will be really helpful

@ashish986
Copy link

ashish986 commented Jul 19, 2020 via email

@ocram ocram added the question label Jul 19, 2020
@ocram
Copy link
Contributor

ocram commented Jul 19, 2020

Is that download a HTTP POST download perhaps?

Android’s DownloadManager is designed to work with downloads that (a) don’t require any cookies and (b) are performed via HTTP GET. Cookie support can be added easily, as can be seen in one of the comments above, but that still leaves us with the restriction to the GET request method.

If your download requires HTTP POST, you should still be able to use the onDownloadRequested callback, but not the DownloadManager.Request instance inside. Just check if the URL passed to that callback is actually valid. If it is, replace the DownloadManager.Request instance with your own code for a HTTP request that downloads and stores the file from the specified URL.

Does that help?

@suvDev
Copy link

suvDev commented Jul 21, 2020

@ashutoshnagar No.

@ashutoshnagar
Copy link

@ocram Yes, its a POST request. Can you please help me understand in detail or share some link which I can refer. New to coding world.

@ashutoshnagar
Copy link

@ashish986 Can we open browser link on DownloadRequested and download the file and come back on app without user noticing it or can we control the custom tab functionality from app, like we can do for webview?

@ashish986
Copy link

ashish986 commented Jul 21, 2020 via email

@ashutoshnagar
Copy link

Thanks @ashish986 for quick reply, can we control the flow through app, just like we do in webview using js because somewhere i had read there is no js bridge between custom tab and app?

@ashish986
Copy link

ashish986 commented Jul 21, 2020 via email

@ocram
Copy link
Contributor

ocram commented Jul 22, 2020

I would listen to the onDownloadRequested callback as usual, and instead of using a DownloadManager instance there, with the url given, use HttpClient and HttpPost as shown here while removing the three (!) lines for HTTP GET from that example so that you actually store the response (HttpResponse) of the HTTP POST request.

Does that help?

If your HTTP POST request requires parameters, though, just using the url from onDownloadRequested will not be enough. You should go to your desktop computer, open your web browser, open the developer tools and switch to the network tab to check if the download requires parameters.

@ashutoshnagar
Copy link

@ocram It didnt work, it is still downloading a corrupt zip file

@ocram
Copy link
Contributor

ocram commented Jul 26, 2020

Does it need cookies, or a certain referrer header, or the same user-agent string as with the previous requests perhaps?

@tusharuit25
Copy link

does anyone got solution for this in flutter ?

@Atindrasharma
Copy link

Any solution now in web view I also facing this

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

No branches or pull requests