0

I use the below code in mvc to download Excel file but it shows error query string too long.

public ActionResult Download(string input)
{
    Response.Clear();
    Response.ClearHeaders();
    Response.ClearContent();
    Response.Buffer = true;
    Response.AddHeader("Content-Disposition", "attachment; filename= download.xlsx");
    Response.AddHeader("Content-Type", "application/Excel");
    Response.ContentType = "application/vnd.ms-excel";
    Response.WriteFile(input);
    Response.End();

    return Content(String.Empty);
}
ekad
  • 14,436
  • 26
  • 44
  • 46
  • 1
    whats the file name? can you copy it to a temp location with a shorter file path name? – DLeh Dec 03 '14 at 15:27
  • also you should use the `File()` method instead of writing the response directly. Usage: `Download(string input) { return File("download.xlsx"); }` – DLeh Dec 03 '14 at 15:28
  • 1
    2 questions...1 - what line produces the error? 2 - how does the Download action get called? If you're trying to call Download using HttpGet and passing the entire excel file as a parameter that will likely be your problem. – DoctorMick Dec 03 '14 at 15:30

1 Answers1

0

This code works for me for PDF:

public FileStreamResult DownnloadPDF(int id)
        {Document document = new Document();

            MemoryStream stream = new MemoryStream();
                PdfWriter pdfWriter = PdfWriter.GetInstance(document, stream);
                pdfWriter.CloseStream = false;

                document.Open();

                formatPDF(document, model);

document.Close();

            stream.Flush(); //Always catches me out
            stream.Position = 0; //Not sure if this is required

            return File(stream, "application/pdf", "title" + ".pdf");
        }

I really think that your ActionResult will not work.

clement
  • 4,204
  • 10
  • 65
  • 133
  • I use the File stream It creates same error query string too long. Response.WriteFile works fine IE11 & firefox but not in chrome – Gurpreet Chhabra Dec 04 '14 at 19:02
  • @GurpreetChhabra if the querystring os too long, I suggest you tu use HTTPOST insteread of the implicit HTTPGET if it is possile for you – clement Dec 05 '14 at 08:00