简单的问题。我在mojolicious应用中生成了一个.doc文件。我要下载。这是我的问题,如何获取浏览器进行下载?
我正在使用CPAN模块MsOffice :: Word :: HTML :: Writer生成文档。
这是我的mojolicious应用程序中的子例程,它由Jquery中的Ajax请求调用:
sub down_doc {
my $self = shift;
my $doc = MsOffice::Word::HTML::Writer->new(
title => "My new Doc",
WordDocument => {View => 'Print'},
);
$doc->write("Content and Stuff");
my $save = $doc->save_as("/docs/file.doc");
$self->res->headers->content_disposition("attachment;filename=file.doc");
$self->res->headers->content_type('application/msword');
$self->render(data => $doc->content);
}
这是我在Jquery中的Ajax请求:
var request = $.ajax({
url: "/down_doc",
type: "post",
data: {'data': data},
});
request.done(function(response, textStatus, jqXHR) {
window.location.href = response;
});
我知道我的Ajax“完成”处理程序是错误的,我只是在尝试。如何使网页提示保存和下载.doc文件?
在服务器端,这确实不是问题,但是,如果不使用(相对较新的)File API,就无法保存来自ajax请求的响应。我建议用一种临时形式替换ajax:
$('<form method="post" action="/down_doc">')
.append(
$('<input type="hidden" name="data">')
.attr("value", JSON.stringify(data))
)
.appendTo('body')
.submit();
提交表单后,/ down_doc处理程序将使用适当的content-disposition标头和文档数据进行答复,浏览器将完成处理文件保存的工作。
如果您不打算在请求后使用服务器上的文件,则可以删除以下行:
my $save = $doc->save_as("/docs/file.doc");
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句