[英]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".我坚持执行问题的下载部分,我有一个 AJAX 调用 PHP 只返回一个文件,但类型是“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.
最初的计划是在目录上执行 scandir 以获取所有文件名并检查最新的文件名并将其与应用程序进行比较。
On the Xamarin.Form my code is在 Xamarin.Form 上我的代码是
_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.所以这个想法是,当用户启动应用程序时,它将调用 GetUpdates。 For now GetUpdate is tied to a button click event.
现在 GetUpdate 与按钮单击事件相关联。 So the idea on the code is that it will download the file via HTTPClient then write it on a Downloads folder.
所以代码的想法是,它将通过 HTTPClient 下载文件,然后将其写入下载文件夹。
In this state that code doesn't work在此 state 中,该代码不起作用
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.由于使用内容类型“application/octet-stream”进行字节下载在 Xamarin.Forms 上不可靠,我想出了另一种下载和安装 apk 文件的方法。
Instead of capturing the data, storing it locally then running it afterwards, I changed that sequence to the API call to a default browser我没有捕获数据,将其存储在本地然后运行它,而是将该序列更改为对默认浏览器的 API 调用
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.所以这个想法是应用程序将在浏览器中下载并让用户打开它并安装更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.