Как скачать сгенерированный файл Java Script в веб-представлении Xamarin - PullRequest
0 голосов
/ 07 сентября 2018

У меня есть веб-страница с текстовым полем и кнопкой загрузки, когда нажата кнопка, содержимое этого текстового поля будет загружено в виде текстового файла.Я могу загрузить эту веб-страницу в веб-браузере Android, но пока нажимаю кнопку загрузки, она ничего не делает.

Итак, я попытался с помощью прослушивателя загрузки Android WebView, этот метод запускается во время начала загрузки на веб-странице.Но он генерирует недопустимый аргумент исключения при создании DownloadManager.Request с этим URI.Но если я дам онлайн загружаемую ссылку (http://25.io/toau/audio/sample.txt), она будет работать нормально. Найдите веб-страницу и загрузите код прослушивателя ниже.

Код веб-страницы

<script language="Javascript" >
function download(filename, text) {
  var pom = document.createElement('a');
  pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + 

encodeURIComponent(text));
  pom.setAttribute('download', filename);

  pom.style.display = 'none';
  document.body.appendChild(pom);

  pom.click();

  document.body.removeChild(pom);
}
</script>
<style>
form * {
  display: block;
  margin: 10px;
}
</style>
<html>
<body>

<form onsubmit="download(this['name'].value, this['text'].value)">
  <input type="text" name="name" value="test.txt">
  <textarea rows=3 cols=50 name="text">Please type in this box. When you 

click the Download button, the contents of this box will be downloaded to 

your machine at the location you specify. </textarea>
  <input type="submit" value="Download">
</form>
</body>
</html>

Код WeBView

public void OnDownloadStart(string url, string userAgent, string contentDisposition, string mimetype, long contentLength)
    {
        //for downloading directly through download manager
        global::Android.Net.Uri uri = global::Android.Net.Uri.Parse(url);
        Request request = new Request(uri);
        request.AllowScanningByMediaScanner();
        request.SetNotificationVisibility(DownloadManager.Request.VisibilityVisibleNotifyCompleted);
        request.SetDestinationInExternalPublicDir(Environment.CurrentDirectory, "MyFile.txt");
        DownloadManager dm = DownloadManager.FromContext(Forms.Context);
        dm.Enqueue(request);
    }
...