Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8718641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:35:49+00:00 2026-06-13T06:35:49+00:00

I am new to using ASP.NET MVC 4 with Web Api. I want to

  • 0

I am new to using ASP.NET MVC 4 with Web Api.

I want to allow user to download a file, this file I will be creating on the server side. For creating the file I have managed to get hold of the following code

[ActionName("Export")]
public HttpResponseMessage PostExportData(SomeModel model)
{           
    string csv = _service.GetData(model);
    HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
    result.Content = new StringContent(csv);
    //a text file is actually an octet-stream (pdf, etc)
    result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
    //we used attachment to force download
    result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
    result.Content.Headers.ContentDisposition.FileName = "file.csv";
    return result;            
}

HOW TO CALL THIS WEB API METHOD USING JQUERY ?

But I am not sure of how to call this web api using jquery and make it return me a file, with a “save as / open” option that you normally get when downloading any file.

Can some one please help me and guide me in how to make the call and download the file. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:35:50+00:00Added an answer on June 13, 2026 at 6:35 am

    You can do something like this inside the view where you want to use jquery. I assume the name of the controller is ExportController. You also have to break down the model variables, or alternatively collect the model inside HttpResponseMessage PostExportData(SomeModel model)
    via some other way.

    html:

    <a class="export">export</a>
    

    javascript:

    <script>
    $('a.export').click(function(e) {
        e.preventDefault();  //stop the browser from following
        window.location.href = '@Url.Action('Export', 'ExportController', new { property = model.property, property = model.property2 })';
    });
    </script>
    

    To use POST

    function UpdateForm(modelObject) {
       if ($('#hidden-form').length < 1)
       {
           $('<form>').attr({
               method: 'POST',
               id: 'hidden-form',
               action: '@Url.Action('Export', 'Export')'
           }).appendTo('body');
       }
       $('#hidden-form').html('');
       for(var propertyName in modelObject) {
           $('<input>').attr({
                type: 'hidden',
                id: propertyName,
                name: propertyName,
                value: modelObject[propertyName]
           }).appendTo('#hidden-form');
        }
    }
    
    $('a.export').click(function(e) {
        e.preventDefault();
        var modelObject = { property1 : "property1" };
        UpdateForm(modelObject);
        $('#hidden-form').submit();
    });
    

    Then you can just post #hidden-form via js which will trigger the file download

    Update: This is a full example for posting, and its not checked for typos etc so debug any minor errors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a web service using ASP.NET MVC's new WebAPI that will serve
I will start new project which is based on ASP.NET MVC 3 using C#
I'm creating new Site Definitions using this method: http://weblogs.asp.net/paulballard/archive/2007/04/09/creating-a-custom-sharepoint-2007-portal-site-definition-using-the-portalprovisioningprovider-class.aspx and when they get created,
I am using web api with ASP.NET MVC 4. I have the following named
I'm implementing a web API using the REST for ASP.NET MVC framework (MVC 2).
I using a reCaptcha on my web page under asp.net mvc. This web site
I am developing some sort of service using asp.net mvc 4 web api. On
I am new to ASP.Net MVC and ASP.Net web api. I have worked on
I new to using ASP.net-mvc and would like some help. I created a web
I'm creating a web service (asmx) using asp.net mvc 3 and it has a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.