2010-04-14 44 views
0

I am developing a web-app where I want to provide a download link to users - so they can download a zip file from my server. Now, my requirement is that I want to execute some processing logic contained in a servlet before displaying the file dialog to the user for 'zip' files.在html <a href> tag, how to call servlet before opening file dialog for download (save as)?

So If I write

<a href="abc.zip".......> 

then it opens a file dialog asking the user to select the location where this file can be saved

But if I want a servlet's doGet method to do some preprocessing - say like building the zip file , then how can I first call the servlet and then open the file dialog.

Will the following snippet work?

<a href="MyHandlerServlet;abc.zip".......> 

Thanks for your help !!

回答

2

I would rather pass the filename as pathinfo instead of request parameter, e.g.

<a href="MyHandlerServlet/abc.zip"> 

Otherwise a certain widely used webbrowser developed by a team in Redmond (cough) would use MyHandlerServlet as filename during Save As. When passing the filename as pathinfo, you can obtain the requested file in the servlet by:

String filename = request.getPathInfo(); 

You only need to map the servlet on /MyHandlerServlet/* instead of /MyHandlerServlet. Also see this basic servlet example

+0

感謝BalusC的建議...現在工作正常:) – deepthinker121 2010-04-16 08:18:05

1
<a href="MyHandlerServlet?file=abc.zip"> 

The servlet would need to respond to the request with the zip file.

+0

嗨大衛, 當我嘗試這樣做,它不會打開文件對話框並進入servlet的doGet方法。 如何顯示文件對話框給用戶以支持'另存爲'操作? – deepthinker121 2010-04-14 10:05:08

+2

@ deepthinker121您的doGet方法應該向客戶端返回一個HTTP響應,其中包含生成的壓縮文件。這會自動觸發用戶的瀏覽器顯示文件保存對話框。 – Daan 2010-04-14 10:08:32

+0

@Daan 非常感謝.. :)這工作對我來說 - 我作爲HTTP響應的一部分返回文件.. – deepthinker121 2010-04-14 11:16:01

相關問題