window.href.location不能在chrome中工作 [英] window.href.location not working in chrome
问题描述
请在标记为重复之前阅读此问题。我试图通过使用Javascript重定向到ASP.NET MVC3项目中的控制器动作来下载文件。它适用于Firefox和IE8。这里是我的Javascript代码:
< script type =text / javascript>
$(function(){
$(。pdf)。click(function(e){
e.preventDefault();
$ .post(@ Url .Action(PDF,Helper),{id:@ Model.ID},function(data){
if(data.message ==File not found。){
alert(data.message);
} else {
location.href ='@ Url.Action(PDF,Helper,new {id = Model.ID})';
}
});
});
});
< / script>
以下是我试过的:
-
我将
返回false;
与所有以下内容结合在一起 我将该行更改为: -
我试过
window.location
而不是<$ c $> -
我将相关链接改为完整链接
-
我添加了这一行:
window.event.returnValue = false;
它给出错误window.event未定义。我用
e
替换了它。 我写了 http://www.google.com 取而代之,它也不起作用。但是,它在单独的html文件中工作,只包含location.href =http://www.google.com;
。
window.location.href =../../Helper/PDF/@Model。 ID;
<$> c $ c> setTimeout(function(){
document.location.href ='@ Url.Action(PDF,Helper,new {id = Model.ID})'
}, 500);
所有这些都可以在IE8和Firefox中使用,但不能在Chrome中使用。我能做些什么来解决这个问题?
编辑:
这是生成的脚本:
$(function(){
$(。pdf)。click(function(e){
e.preventDefault();
$ .post(/ Helper / PDF,{id:130405002},function(data){
if(data.message ==File not found。){
alert (data.message);
} else {
location.href ='/ Helper / PDF / 130405002';
}
});
});
});
这是生成的HTML,我称之为函数:
< input class =pdftype =buttonvalue =下载档案/>
调试动作我得到了这些结果;在控制台状态是失败。 类型
是预期的application / pdf, Size
是198 kb,它是文件大小。方法是 POST
。
这是我的控制台输出:
我改变了以下脚本:
< script type =text / javascript>
$(function(){
$(。pdf)。click(function(){
$ .get('@ Url.Action(PDFAvailable,Helper) ',{id:@ Model.ID},function(data){
if(data.message ==File not found。){
alert(data.message);
} else {
location.href ='@ Url.Action(PDF,Helper,new {id = Model.ID})';
}
});
});
});
< / script>
你看,我添加了一个新的控制器动作,它只控制文件是否存在并调用文件行动。这适用于所有浏览器,至少是我拥有的浏览器。我不完全知道,但这可能是Chrome中的一个错误或安全预防措施。
Please read the question before flagging as duplicate. I'm trying to download a file by redirecting with Javascript to a controller action in my ASP.NET MVC3 project. It works perfectly in Firefox and IE8. Here's my Javascript code:
<script type="text/javascript">
$(function(){
$(".pdf").click(function(e) {
e.preventDefault();
$.post("@Url.Action("PDF","Helper")", {id : @Model.ID} ,function(data){
if(data.message == "File not found.") {
alert(data.message);
} else {
location.href = '@Url.Action("PDF", "Helper", new { id = Model.ID })';
}
});
});
});
</script>
Here's what I tried:
I added
return false;
with all the things belowI changed the line to:
window.location.href = "../../Helper/PDF/@Model.ID";
I tried
window.location
insteadI changed relative link to full link
Tried without
window
Tried:
setTimeout(function() { document.location.href = '@Url.Action("PDF", "Helper", new { id = Model.ID })' },500);
I added the line:
window.event.returnValue = false;
it gave the error "window.event is not defined". I replaced it with
e
.I wrote "http://www.google.com" instead, and it didn't work too. However, it worked in a separate html file only consists of
location.href = "http://www.google.com";
.
All of this worked in IE8 and Firefox but not in Chrome. What can I do to resolve this?
EDIT: This is generated script:
$(function(){
$(".pdf").click(function(e) {
e.preventDefault();
$.post("/Helper/PDF", {id : 130405002} ,function(data){
if(data.message == "File not found.") {
alert(data.message);
} else {
location.href = '/Helper/PDF/130405002';
}
});
});
});
This is the generated HTML where I call the function:
<input class="pdf" type="button" value="Download File" />
Debugging the action I got these results; at the console Status is "failed". Type
is "application/pdf" as expected and Size
is 198 kb, it's the file size. Method is POST
.
This is my console output:
I changed the script with the following:
<script type="text/javascript">
$(function () {
$(".pdf").click(function () {
$.get('@Url.Action("PDFAvailable", "Helper")', { id: @Model.ID }, function (data) {
if(data.message == "File not found.") {
alert(data.message);
} else {
location.href = '@Url.Action("PDF", "Helper", new { id = Model.ID })';
}
});
});
});
</script>
You see, I added a new controller action, which only controls if the file exists and call the file action. This works across all browsers, at least the ones I have. I don't exactly know, but this might be a bug or security precaution in Chrome.
这篇关于window.href.location不能在chrome中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!