简体   繁体   中英

Downloading a file with a type 'application/octet-stream' and saving it download folder to install

I'm currently doing a proof of concept for my application to check for updates and if their is a new version then download that version and install it (prompt the user to install?).

I'm stuck on implementing the download part of the problem, I have an AJAX call with PHP that just returns a file but the type is "application/octet-stream".

<?php @session_start();

$filename = "\\xxx\x\Application\ka-v1.0.0.0.txt";

header("X-Sendfile: $filename");
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="' . basename($filename) . '"');

?>

The idea is to expand this and check the database what is the latest version and based on that it will form the filename. Originally the plan is to do a scandir on the directory to get all filenames and check for the latest and compare that to the application.

On the Xamarin.Form my code is

        _client = new HttpClient();
    public async void GetUpdate(string uri)
    {
        var bytesme = await DownloadFile("https://10.0.2.2:8080/apps/x/ajax/ajax.get_download.php");
        File.WriteAllBytes(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), bytesme);
    }
    
    public static Task<byte[]> DownloadFile(string url)
    {
        if (!url.Trim().StartsWith("https", StringComparison.OrdinalIgnoreCase))
            throw new Exception("iOS and Android Require Https");

        return _client.GetByteArrayAsync(url);

    }

So the idea is that when the user starts the application then it will call GetUpdates. For now GetUpdate is tied to a button click event. So the idea on the code is that it will download the file via HTTPClient then write it on a Downloads folder.

In this state that code doesn't work

The next step that I haven't research is automatically opening the file to prompt the user to install the new update

Since doing byte download with content-type 'application/octet-stream' is unreliable on Xamarin.Forms I came up with an alternative way of downloading and installing a apk file.

Instead of capturing the data, storing it locally then running it afterwards, I changed that sequence to the API call to a default browser

        try
        {
            await Browser.OpenAsync("https://10.0.2.2/apps/x/ajax/ajax.get_download.php", BrowserLaunchMode.SystemPreferred);
        }
        catch (Exception ex)
        {
            // An unexpected error occured. No browser may be installed on the device.
        }

So the idea is that the application will get downloaded in the browser and will give the user to open it and install the update.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM