0

How to send a generated txt to the client?

asked 2009-02-03 02:16:32 +0800

judacoor gravatar image judacoor
280 1 3 8

Hey everyone,

I really don't know how to do this, I presume it's with server push enabled or something, but I was hoping you guys could point me in the right direction.

My zk webapp should generate a txt file according to some input from the user and some data obtained from the DB. But I've only done small exercises regarding txt file generation, where you set a path in your hard drive and then just write into the txt with a fileWriter. But this time it's only a txt file that needs to be generated and sent to the user, but I do not need to store it, so my first question is:

1. How to generate the txt file but not have it stored on the server's drive? Should I do it normally and then erase it?

And my second question is:

2. How to, after having the txt ready, send it to the user via pop-up download? Like when you download a normal file and the browser asks you if you wanna open it or save it, and well...you know.

I checked the ZK Explorer out, and for now I've thought about something like generating the txt file with a unique id in its name (so it won't be a problem if two users wanna do it simultaneously) and save in a default path in the server, the use Filedownload.save("/defaultTxtDir/0001.txt", null); to send the file to the user. After that, erasing the txt file.


What would be the best practice?

Thank you 1000 times,


judacoor

delete flag offensive retag edit

4 Replies

Sort by ยป oldest newest

answered 2009-02-06 11:34:58 +0800

PeterKuo gravatar image PeterKuo
481 2

As you can see in Filedownload.java, one api is
save(byte[] content, String contentType, String flnm)

No actual file in drive need. byte[] can be accepted.

link publish delete flag offensive edit

answered 2009-02-07 16:06:06 +0800

judacoor gravatar image judacoor
280 1 3 8

thanks PeterKuo!

Working perfectly!

Judacoor

link publish delete flag offensive edit

answered 2009-02-07 16:21:23 +0800

judacoor gravatar image judacoor
280 1 3 8

Hello again,

And if I need to send multiple txt files to the user....how do I do that? Can I create a zipped folder or something? Or how do I send one by one?

I've tried:

String date = Calendar.getInstance().getTime().toString();
		String user = "John\r\n";
		String salute = "Good morning!\r\n";		
		for (int i = 0; i < 5; i++) {
			String filename = "HelloWorld" + i + ".txt";
			date = Calendar.getInstance().getTime().toString();
			Filedownload.save(salute + user + date, null, filename);
			try {
				Thread.currentThread();
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

But it only asks me to save the last one....

Thanks for your help!

link publish delete flag offensive edit

answered 2009-02-09 04:20:20 +0800

PeterKuo gravatar image PeterKuo
481 2

For better user experience, it's suggested download one file at one click.
Thus in ZK implementation, use Filedownload as in your case,
will only show the last one file to save.

You can find how to zip files by java easily by google.

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2009-02-03 02:16:32 +0800

Seen: 285 times

Last updated: Feb 09 '09

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More