2014-12-03 195 views
0

我在mvc中使用下面的代码来下载Excel文件,但它显示错误查询字符串太长。错误:http查询字符串太长

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); 
} 
+1

什么是文件名?你能把它复制到一个较短的文件路径名称的临时位置吗? – DLeh 2014-12-03 15:27:45

+1

什么是问题? – Eun 2014-12-03 15:28:01

+0

也应该使用'File()'方法而不是直接写入响应。用法:'下载(字符串输入){返回文件(“download.xlsx”); }' – DLeh 2014-12-03 15:28:59

回答

0

此代码的工作对我来说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"); 
     } 

我真的觉得你的ActionResult将无法正常工作。

+0

我使用文件流它创建相同的错误查询字符串太长。 Response.WriteFile工作正常IE11&Firefox,但不在铬 – 2014-12-04 19:02:01

+0

@GurpreetChhabra如果查询字符串操作系统的操作系统太长,我建议你使用HTTPOST insteread的隐式HTTPGET,如果它是possile你 – clement 2014-12-05 08:00:28