简体   繁体   English

使用Javascript Ajax调用来调用MVC操作以下载对象

[英]Using Javascript Ajax call to call MVC action to download object

Ive created a simple MVC controller action that creates a simple ics (calendar) item and sends it back through the controller action. Ive创建了一个简单的MVC控制器动作,该动作创建了一个简单的ics(日历)项,并通过该控制器动作将其发送回去。 As follows: 如下:

    public object GenerateICS(int myID)
    {
        iCalendar iCal = new iCalendar();            
         Event evt = iCal.Create<Event>();
        Uri eventLink = new Uri("http://localhost:");
        evt.IsAllDay = false;

       evt.Start = new iCalDateTime(DateTime.Now);
       evt.End = new iCalDateTime(DateTime.Now.AddDays(3));

       evt.Summary = "MySummary";
       evt.Url = eventLink;
       evt.Description = "You know it";         

      Response.ContentType = "text/v-calendar";
      Response.AddHeader("content-disposition", "attachment; filename=" + "Event" + ".ics");
      iCalendarSerializer serializer = new iCalendarSerializer(iCal);
      string result = serializer.SerializeToString(iCal);
      Response.Write(result);          
        return Response;
    }

So with the website running, if I go to: 因此,在运行网站的情况下,如果我转到:

http://localhost:21312/GenerateICS?myID=1 

This will generate the ics file server side and pass it back to the client, so your receive a "Do you want to open blah.ics from localhost?". 这将生成ics文件服务器端,并将其传递回客户端,因此您将收到“是否要从本地主机打开blah.ics?”。 This is perfectly what I want. 这正是我想要的。

My issue is how do I achieve exactly the same by executing it from javascript. 我的问题是如何通过从javascript执行代码来实现完全相同的效果。 I have the following ajax call: 我有以下ajax电话:

 $.ajax({
                url: "app/GenerateICS",
                data: { myID: 1 },
                success: function (data) {
                    //call is successfully completed and we got result in data
                    alert(data);
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    //some errror, some show err msg to user and log the error  
                    alert(xhr.responseText);

                }
            });

This executes the mvc controller perfectly. 这样可以完美执行mvc控制器。 But it returns the ics response to the success function. 但是它将ics响应返回给成功函数。 How do I call the controller with ajax but have it so the file is downloaded as I describe how it does when you manually do it? 我如何用ajax调用控制器,但有该文件,以便下载文件,因为我描述了手动执行该操作的过程?

Thanks 谢谢

Thanks to @heads5150 for the link. 感谢@ heads5150提供的链接。

It was just the fact of setting the browser location with: 仅仅是通过以下方式设置浏览器位置:

document.location.href = "app/GenerateICS?...";

In your ajax success , put this 为了您的ajax成功,请把这个

  Window.location.href='yourICSfileLink';

This would redirect the browser on success of the ajax creating the file to then open or in this case download the ics file, note this would happen each time ajax success occured 这将在ajax成功创建后将浏览器重定向到创建文件,然后打开该文件或下载ics文件,请注意,每次ajax成功发生都会发生这种情况

I was just looking for downloading from js and the answer is generally related to creating an iframe and doing through it. 我只是想从js下载,答案通常与创建iframe并完成它有关。 There are even some jquery plugins that do so. 甚至还有一些jquery插件可以这样做。 Some eg: 一些例如:

Download File Using Javascript/jQuery 使用Javascript / jQuery下载文件

http://johnculviner.com/category/jQuery-File-Download.aspx http://johnculviner.com/category/jQuery-File-Download.aspx

If you google it you can find even more about the topic. 如果您使用Google搜索,则可以找到有关该主题的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM