ファイルのダウンロード
POIで作成したExcelファイルをダウンロードするような処理を実装するには、BinaryResponseクラスを使用します。今回は、アップロードしたファイルをそのままダウンロードする処理を作成してみます。この処理はあまり意味があるものではないのですが、ファイルダウンロードの実装例として参考にしてください。
まず、以下の通りFileUploadPage.htmlにuploadAndDownloadButtonを追加します。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id="mainDiv">
<form id="fileUploadForm">
<div class="formHeader">xxx</div>
・・・ 中略 ・・・
<button id="uploadButton" type="button">アップロード</button>
<button id="uploadAndDownloadButton" type="button">アップロードしたファイルをダウンロード</button>
</form>
</div>
</body>
</html>
次に、FileUploadForm.jsにuploadAndDownloadButtonのクリックイベントハンドラを作成します。FileUploadForm.jsのattachメソッドで、uploadAndDownloadButtonがクリックされたとき、uploadAndDownload()メソッドを呼び出すようにします。uploadAndDownload()メソッドでは、submitForDownloadメソッドを使用して、サーバーのuploadAndDownloadを呼び出します。
/**
* @fileOverview {@link FileUploadForm}クラスを記述したファイルです。
*/
/**
* @class FileUploadForm
*
* @extends Form
*/
FileUploadForm = createSubclass("FileUploadForm", {}, "Form");
/**
* HTMLエレメントとの対応付けを行います。
*/
FileUploadForm.prototype.attach = function() {
Form.prototype.attach.call(this);
var thisForm = this;
this.find("#uploadButton").click(function() {
thisForm.upload();
});
this.find("#uploadAndDownloadButton").click(function() {
thisForm.uploadAndDownload();
});
};
・・・ 中略 ・・・
/**
* アップロードしたファイルをダウンロードボタンの処理。
*/
FileUploadForm.prototype.uploadAndDownload = function() {
// エラー情報をクリアする。
currentPage.resetErrorStatus();
if (this.validate()) {
var thisForm = this;
this.submitForDownload("uploadAndDownload", function(r) {
if (r.status == ServerMethod.INVALID) {
// サーバーのバリデーション結果を表示する。
currentPage.setErrorInfo(thisForm.getValidationResult(r), thisForm);
}
});
}
};
FileUploadForm.javaには、以下の通りuploadAndDownloadメソッドを実装します。uploadAndDownloadメソッドは、バリデーションを行い問題があった場合その一覧をJsonResponseで返します。バリデーションで問題が無かった場合、アップロードされたファイルをBinaryResponseに設定したものを返します。Form.jsのsubmitForDownloadメソッドは、BinaryResponseを受け取った場合そのファイルをダウンロードし、JSONを受け取った場合その内容を応答処理に渡します。
package pagesample.page;
・・・ 中略 ・・・
/**
* ファイルアップロードフォーム。
*
*/
public class FileUploadForm extends Form {
/**
* コンストラクタ。
*/
public FileUploadForm() {
super(null);
this.addField(new TextField("text1")).setComment("テキスト");
this.addField(new FolderStoreFileField("file1")).addValidator(new RequiredValidator());
}
/**
* Logger.
*/
private Logger logger = Logger.getLogger(FileUploadForm.class);
・・・ 中略 ・・・
/**
* アップロードしたファイルをダウンロードするメソッド。
* @param p POSTされた情報。
* @return 応答情報。
* @throws Exception 例外。
*/
@WebMethod
public Response uploadAndDownload(final Map<String, Object> p) throws Exception {
this.methodStartLog(logger, p);
Response result = null;
List<ValidationError> list = this.validate(p);
if (list.size() > 0) {
result = new JsonResponse(JsonResponse.INVALID, list);
} else {
Map<String, Object> d = this.convertToServerData(p);
String text1 = (String) d.get("text1");
FileObject file1 = (FileObject) d.get("file1");
File f = file1.getTempFile();
logger.debug("text1=" + text1 + "file=" + f.getAbsolutePath());
result = new BinaryResponse(file1.getTempFile().getAbsolutePath(), "application/octet-stream", file1.getFileName());
}
this.methodFinishLog(logger, result);
return result;
}
}
