从ajax调用打开文件?
我试图通过 ajax 调用打开文件,但它打不开?当我直接在浏览器中输入它(http://localhost/home/showfile)时它可以工作吗?
<script type="text/javascript">
$.ajax({
type: 'POST',
url: "/Home/ShowFile"
})
</script>
public ActionResult ShowFile()
{
return File(@"C:\\development\\FileOpen\\FileOpen\\TextFile1.txt", "application/octet-stream", Server.HtmlEncode("TextFile1.txt"));
}
i am trying to open a file from an ajax call, but it does not open? it works when i type it directly in the browser(http://localhost/home/showfile)?
<script type="text/javascript">
$.ajax({
type: 'POST',
url: "/Home/ShowFile"
})
</script>
public ActionResult ShowFile()
{
return File(@"C:\\development\\FileOpen\\FileOpen\\TextFile1.txt", "application/octet-stream", Server.HtmlEncode("TextFile1.txt"));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
获取文件后,并不清楚您要对文件执行什么操作,但您可以首先向
$.ajax
添加回调来执行 < em>返回数据的东西。例如:你真的需要 HTTP post 吗?您没有通过请求发送任何内容,那么为什么不直接使用 HTTP get 呢?
It's not terribly clear what you're trying to do with the file once you've fetched it, but you can start with adding a callback to
$.ajax
to do something with the data returned. For example:Do you really need an HTTP post, though? You're not sending anything with the request, so why not just use an HTTP get?
首先,由于同源策略,您需要确保该文件位于本地服务器上。然后你可以使用 jQuery load 之类的东西来加载文件。
First, you need to make sure the file is on your local server because of the same origin policy. Then you can use something like jQuery load to load in the file.