与API控制器文件下载问题 [英] File download issue with api controller

查看:204
本文介绍了与API控制器文件下载问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class DefaultController : Controller
{
    // GET: Default
    public ActionResult Index()
    {
        return Download();
    }

    public FileResult Download()
    {
        string xmlString = "my test xml data";
        string fileName = "test" + ".xml";
        return File(Encoding.UTF8.GetBytes(xmlString), "application/xml", fileName);
    }
}

我在asp.net MVC应用程序上面code下载文件。作为我控制器继承控制器它工作得很好。但是,当我提出这个code到的WebAPI控制器,​​它在回抛出错误的文件。经过分析,我发现我在的WebAPI控制器继承到ApiController(system.web.http.api控制器)。我发现,在没有ApiController File类。有没有办法实现的WebAPI控制器下载文件功能的任何选项?

I have the above code in asp.net mvc application to download a file. It worked fine as my controller is inherited to Controller. But when I move this code to Webapi controller it throws error at return File. After analysis I found that my controller in webapi is inheriting to ApiController(system.web.http.api controller). I found that there is no File class in ApiController. Is there any option to implement downloading file functionality in webapi controller?

我试过的WebAPI控制器的下方替代code,但不可能看到一个下载文件一次我称之为

I tried the below alternative code in webapi controller but couldnt see a downloading file once I call this.

public HttpResponseMessage DownloadConstructedXmlFile()
        {
            var result = new HttpResponseMessage(HttpStatusCode.OK);
            string xmlContent = "My test xml data";
            //var serializer = new XmlSerializer(typeof(xmlContent));

            var builder = new StringBuilder();
            using (var writer = new StringWriter(builder))
            {
                // serializer.Serialize(writer, xmlContent);
                result.Content = new StringContent(xmlContent, Encoding.UTF8, "application/xml");
                result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = string.Format("test.xml")
                };
               // return result;
            }

            return new HttpResponseMessage();
        }

PS:我想使用angularjs code调用这个API通过角service.This上调用下载按钮点击。任何样品角code或帮助API控制器code或建议将是有益的。

PS: I am trying to use angularjs code to call this api through angular service.This is invoked on download button click. Any sample angular code or help in api controller code or suggestions would be helpful.

推荐答案

下面是一个非常简单的例子:

Here is a much simpler example:

   using System.Net;
   using System.Net.Http;
   using System.Net.Http.Headers;
   using System.Text;
   using System.Web.Http;

   namespace WebApplication1.Controllers
   {
    public class ValuesController : ApiController
    {
        // GET: api/Values
        public HttpResponseMessage Get()
        {
            var xmlString = "<xml><name>Some XML</name></xml>";
            var result = Request.CreateResponse(HttpStatusCode.OK);
            result.Content = new StringContent(xmlString, Encoding.UTF8, "application/xml");
            result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = "test.xml"
            };

            return result;
        }
    }
   }

这篇关于与API控制器文件下载问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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