作者wind681201 (阿风~~)
看板Ajax
标题[问题] 下载档案
时间Sun Jun 1 14:36:25 2014
我有一个网页,是用mvc的方式写的,
我想做的事是,使用者用$.post的方式到一个网页後
那个action会return file回来,那在原页面要能直接
下载或是开启档案,该怎麽做呢?
MVC return file回来是怎样的形式?
而前端该怎麽做接收方式?
例:我在index页面里,有一个按钮,按了会执行
function sendback(){
$.post("getfile",{id:"A"},function(){
在这里,我想做开启档案,或是出现下载
});
}
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 220.135.124.131
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/Ajax/M.1401604589.A.2A2.html
1F:推 mrbigmouth:window.open要下载的网址 server端做处理 response 06/01 15:22
2F:→ mrbigmouth:header 标注正确 浏览器就会自动开起下载 06/01 15:22
3F:→ wind681201:因为会传值回去,window.open只能用get方式,要用post 06/01 18:10
4F:→ wind681201:有看到可以post方式,但这样会换页,我希望不要换页 06/01 18:33
5F:推 mrbigmouth:建一个form method用post target用_blank然後submit 06/01 19:03
6F:→ mrbigmouth:其实直接指location.href就行了 如果content-type是 06/01 19:05
7F:→ mrbigmouth:下载的话我记得是不会换页的 06/01 19:05
8F:→ mrbigmouth:喔要post的话还是得靠form 06/01 19:05
9F:→ mrbigmouth:总之想靠javascript直接写档案是行不通的 安全性问题 06/01 19:06
10F:→ wind681201:我目前是用target=_blank的方式,连结的话,还是会换页 06/01 20:48
11F:推 Peruheru:我是弄一个隐藏的iframe,然後form的target指向他 07/22 11:55
12F:→ Peruheru:把要post资料submit出去之後,只要接收端呈现的是档案 07/22 11:56
13F:→ Peruheru:就会自动下载了 07/22 11:56