[问题] 下载档案

楼主: wind681201 (阿风~~)   2014-06-01 14:36:25
我有一个网页,是用mvc的方式写的,
我想做的事是,使用者用$.post的方式到一个网页后
那个action会return file回来,那在原页面要能直接
下载或是开启档案,该怎么做呢?
MVC return file回来是怎样的形式?
而前端该怎么做接收方式?
例:我在index页面里,有一个按钮,按了会执行
function sendback(){
$.post("getfile",{id:"A"},function(){
在这里,我想做开启档案,或是出现下载
});
}
作者: mrbigmouth (大嘴先生)   2014-06-01 15:22:00
window.open要下载的网址 server端做处理 responseheader 标注正确 浏览器就会自动开起下载
楼主: wind681201 (阿风~~)   2014-06-01 18:10:00
因为会传值回去,window.open只能用get方式,要用post有看到可以post方式,但这样会换页,我希望不要换页
作者: mrbigmouth (大嘴先生)   2014-06-01 19:03:00
建一个form method用post target用_blank然后submit其实直接指location.href就行了 如果content-type是下载的话我记得是不会换页的喔要post的话还是得靠form总之想靠javascript直接写档案是行不通的 安全性问题
楼主: wind681201 (阿风~~)   2014-06-01 20:48:00
我目前是用target=_blank的方式,连结的话,还是会换页

Links booklink

Contact Us: admin [ a t ] ucptt.com