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

  • Home
  • SEARCH
  • 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 6162489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:39:48+00:00 2026-05-23T21:39:48+00:00

I have managed to create the Excel file using HTMLTextwriter, but the returned file

  • 0

I have managed to create the Excel file using HTMLTextwriter, but the returned file is into a Javascript function.

The view code is:

<input type="submit" id = "exportExcelBtn" class = "searchButton" value="Export To Excel" 
onclick= "ExportToExcel();"/>

The JS function then gets the data required for the control action, and then calls the relevant action, as such:

$.ajax({
    type: "GET",
    url: "/Search/ExportToExcel",
    data: { //parameters        },
    error: function (error) {
        alert(error.responseText);
    },
    success: function (data) {
        alert("success");
        alert(data);
    }
});

The Excel file is created, by deriving from ActionResult, and returned back to the JS method. The created Excel file is written to the browser as such:

HttpContext context = HttpContext.Current;
            context.Response.Clear();

            context.Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
            context.Response.Charset = "";

            context.Response.Cache.SetCacheability(HttpCacheability.NoCache);

            context.Response.ContentType = contentType;
            context.Response.Write(content);
            context.Response.End();

How then can I get that file downloaded to the users computer?

I can easily just use File.WriteAllText in the model method, but I feel that is cheating the MVC method?

  • 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-05-23T21:39:48+00:00Added an answer on May 23, 2026 at 9:39 pm

    You can’t use File.WriteAllText in the model.
    That will write it to the server.

    You need to set the location to a URL that returns the contents of the Excel file.
    The correct way to do that in MVC is to return File(content,"application/x-ms-excel", fileName)

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

Sidebar

Related Questions

I have managed to create a custom action in C# using MakeSfxCA which is
I've deconstructed a standard WPF button using Blend and have managed to create a
I have managed to create a ComboBox with treeview as its itempresenter using the
I have a function that takes a url to an Excel file, and then
I'm having a problem with mixing managed and unmanaged code. I have created two
We have an application that is built with Excel as the front end using
I am new to jQuery and have managed to create a tabbed interface with
I have managed to create a 2 axes graph in ReportLab, by overlapping a
I have an excel file that contain columns of data, my target is to
I am new to autotools and have managed to create a, for the moment,

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.