下载文件到客户端使用jQuery ASP.NET MVC应用程序 [英] Downloading a file onto client in ASP.NET MVC application using JQuery

查看:187
本文介绍了下载文件到客户端使用jQuery ASP.NET MVC应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET MVC控制器与需要的文件服务到客户端(浏览器)的方法。控制器动作被调用了JQuery的$就调用。用户需要被提示下载一旦控制器动作完成了文件。

我用Response.Transmitfile / Response.WriteFile在控制方法,但都没有促使我要下载IE浏览器的文件,即使文件被创建,我使用正确的文件路径为好。

当我直接在浏览器中键入URL控制器调用同样的方法,我立即提示下载创建的文件。

任何人都可以让我知道如果有东西在这个流程失踪?

我怀疑这是我打电话的JQuery控制器动作的方式。 如何使用JQuery的Ajax调用的响应,以确保客户端提示用户下载文件

 的[AcceptVerbs(HttpVerbs.Post)
    公众的ActionResult创建presentation(字符串ID)
    {
     //做的东西在这里,服务器本身上创建一个位置中的文件和地点
       字符串文件名= Path.GetFileName(文件路径);
        Response.ContentType =应用/八位字节流;
        System.String disHeader =附件;文件名= \\+文件名+
           \\;
        Response.AppendHeader(内容处置,disHeader);
        FileInfo的fileToDownload =新的FileInfo(文件路径);
        Response.WriteFile(fileToDownload.FullName);
    }

在JavaScript的一面,这是我如何调用控制器动作

 函数创建presentation()
{
//这里的东西
     $阿贾克斯({
   键入:POST,
   网址:HTTP://本地主机:4844 /动作条/创建presentation
   数据:数据
 });
} //函数结束


解决方案

当您使用 $。阿贾克斯,或与此有关的任何其他AJAX机制,你绕来绕去的普通的浏览器文件传输管道。它的主要管道触发的浏览器保存此文件对话框,而不是AJAX之一。

要实现你想要什么,你要使用同步位置的改变,而不是异步之一:而不是使用 $阿贾克斯,只需设置<$ C。 $ C> document.location :

 函数创建presentation()
{
    //剪断code,创建一个映射称为数据
    变种paramSnippets = [];
    对于(数据项)
    {
        paramSnippets.push(项目+=+数据[项目]);
    }    document.location =HTTP://本地主机:4844 /动作条/创建presentation+? + paramSnippets.join(与&amp;);
}

响应编辑,注释:包括例如

I have a ASP.NET MVC controller with a method which needs to serve up a file to the client(browser). The controller action is invoked with a JQuery $.ajax call. The user needs to be prompted to download the file once the controller action has finished.

I used Response.Transmitfile/Response.WriteFile in the controller method but both have not prompted me to download the file in IE browser even though the file has been created and I am using the right file path as well.

When i invoke the same method directly by typing the controller URL in the browser I am prompted immediately to download the file created.

Could anyone let me know if there is something missing in this flow ?

I suspect it is the way I am calling the controller action in JQuery. How do i use the response of the JQuery Ajax call to ensure the client is prompted to download the file ?

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult CreatePresentation(string id)
    {
     //do something here , create the file and place in a location on the server itself
       string filename = Path.GetFileName(filePath);
        Response.ContentType = "APPLICATION/OCTET-STREAM";
        System.String disHeader = "Attachment; Filename=\"" + filename +
           "\"";
        Response.AppendHeader("Content-Disposition", disHeader);
        FileInfo fileToDownload = new FileInfo(filePath);
        Response.WriteFile(fileToDownload.FullName);
    }

On the Javascript side, this is how i invoke the controller action

function CreatePresentation()
{ 
// something here
     $.ajax({
   type: "POST",
   url: "http://localhost:4844/ActionBar/CreatePresentation",
   data:data
 });
} // end of function

解决方案

when you use $.ajax, or for that matter any other AJAX mechanism, you're going around the normal browser file-transfer pipeline. It's the main pipeline that trigger's the browser's Save This File dialog, not the AJAX one.

To achieve what you want, you'll want to use a synchronous location change rather than an asynchronous one: rather than using $.ajax, just set document.location:

function CreatePresentation()
{
    //snip code that creates a map called "data"
    var paramSnippets = [];
    for (item in data)
    {
        paramSnippets.push(item + "="+data[item]);
    }

    document.location = "http://localhost:4844/ActionBar/CreatePresentation" + "?" + paramSnippets.join("&");
}

edited in response to comments: included example

这篇关于下载文件到客户端使用jQuery ASP.NET MVC应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆