1

I am trying to use code modeled after https://stackoverflow.com/a/29702251/9885144 to do an HttpRequest, generate a download link from the returned blob, then automatically download it (all after clicking a button). Here is the function that runs when the button is clicked: (the Converter.toDocx() function runs an HttpRequest.postFormData() in the format I need and returns the future.)

getDocx(String qStr, String aStr) {
  String totalString = qStr + aStr;
  Converter.toDocx(convertURL, totalString).then((HttpRequest req) {
    String contentType = req.getResponseHeader("content-type");
    print(contentType);
    String filename = "download.docx";

    AnchorElement downloadLink =
        new AnchorElement(href: Url.createObjectUrlFromBlob(req.response));
    downloadLink.rel = contentType;
    downloadLink.download = filename;
    var event = new MouseEvent("click", view: window, cancelable: false);
    downloadLink.dispatchEvent(event);
  });
}

It works fine (on Chrome, Firefox, and Edge) when I use webdev serve. It also works fine when deployed on my remote server with webdev build --no-release -o build:web. But it does not work if I drop the --no-release option so I assume something is different about how dart2js handles this versus dartdevc. I plan to add a popup window with a "click here if it doesn't download" link as a workaround, but is there a better option or is this a dart2js bug?

0 Answers0