1

I use "Filedownload.save" to download files with Zk, but i have a problem.

Zk cut my filenames in some characters, for example, if the linema is "FISH#CHIPS.pdf", the file dowload as "FISH.pdf"

Anyone knows how to solve it?

UPDATE:

I have follow the instructions, and i finally see that the server response this JSON:

{"rs":[["download",["/myApp/zkau/view/z_aq5/dwnmed-3/son/FISH#CHIPS.pdf"]]],"rid":9}

And i am lost now, what do Zk with this JSON on the cliente side?

Ildelian
  • 1,278
  • 5
  • 15
  • 38

2 Answers2

3

The official ZK-Bug is tracked as ZK-3809

A server side workaround is the following:

split download code such as ...

Filedownload.save("test content", "text/plain", "test#test.txt");

... into ...

AMedia media = new AMedia("test#test.txt", "txt", "text/plain", "test content");
Clients.response(new AuDownload((DeferredValue) () -> 
    Executions.getCurrent().getDesktop().getDownloadMediaURI(
        media, "test#test.txt").replace("#", "%23")));

... allowing to encode special chars as needed.


UPDATE: ZK-3809 has been fixed and will be included in ZK version 8.5.1

cor3000
  • 936
  • 1
  • 6
  • 16
1

The problem is that # is one of those "reserved characters" that, while valid in a URL, are treated in special ways. Look at this question for more details. My guess is that everything after the # is interpreted as a fragment on the page, and hence ignored in this case.

There are ways to fix this, for example by replacing # by %23. But doing this on the server side when calling Filedownload.save changes the filename to literally FISH%23CHIPS.pdf.

Instead, we can intercept the client side method that downloads the file when the response you showed arrives. This way, zk will still give the file its normal name, and only the download will sanitize the URL. Add this to a script tag or loaded js file:

zk.afterLoad('zk', function() {
    var oldMethod = zAu.cmd0.download;
    zAu.cmd0.download = function(filename) {
        return oldMethod(filename.replace(new RegExp('#', 'g'), '%23'));
    }
});

Then it will download the file with the complete name. You might want to take the extra time and sanitize the other reserved characters as well. Read this wiki article about "percent encoding" for the right codes.

I have also filed a support ticket with zk, I think this should be handled by the client side method out-of-the-box.

Community
  • 1
  • 1
Malte Hartwig
  • 4,477
  • 2
  • 14
  • 30
  • Thank Malte for your answer, but i have implemented the "official" solution. – Ildelian Nov 30 '17 at 12:06
  • That's fine, it's good that you found this issue. This way, we already have a fix for it until they patch it up internally. I'll apply the fix to our code as well ;D – Malte Hartwig Nov 30 '17 at 12:59