Downloading file with webapi and angularjs issue

2,177 views
Skip to first unread message

Swapnika chowdary

unread,
Aug 9, 2015, 7:53:41 PM8/9/15
to AngularJS
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);
    }
}

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?

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: 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. 



I am trying to get data from db, build xml and then download the xml as file to local system on clicking a button through angular js

Sander Elias

unread,
Aug 10, 2015, 10:46:46 PM8/10/15
to AngularJS
Hi Swapnika,

Creating and saving files is possible in most modern browsers. I have had good results with the following snippet on most browsers:

function downloadIt(text, filename, type) {
   
var link = document.createElement("a"); //set up anchor
   type
= type || "text/html"
   link
.setAttribute("target","_blank");
   
if(Blob !== undefined) {
       
var blob = new Blob([text], {type: type});
       link
.setAttribute("href", URL.createObjectURL(blob));
   
} else {
       link
.setAttribute("href","data:text/html," + encodeURIComponent(text));
   
}
    link
.setAttribute("download",filename);
    document
.body.appendChild(link);
    link
.click();
    document
.body.removeChild(link);
};


Has nothing to do with angualr whatsoever tough ;)

Regards
Sander

Reply all
Reply to author
Forward
0 new messages