4

I'm working on MVC 4. I have generated Excel file dynamically using following simple code. My hosting is on Azure.

I have created a Root path and then try to save that Excel file.

Problem is when my ActionResult method response comes back it is giving default popup to open a file but file name is having a GUID instead my provided file name.

Excel file generation code:

Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

// ...
//Save
        LocalResource resource = RoleEnvironment.GetLocalResource("MyValue");
        string tempPath = resource.RootPath + "DemoFile.xls";
return tempPath;

tempPath returns path like C:\AppData\Local\dftmp\Resources\11a2435c-998c-4fe8-aa55-8bb42455b4ca\directory\DemoFile.xls.

The Download File popup does not give file name as DemoFile it gives some GUID why so?

enter image description here

ActionResult method code:

public ActionResult DownloadExcel() {
    string path = ExcelGenerationCode(fileName);
        Stream s = new FileStream(path, FileMode.Open, FileAccess.Read);
        return new FileStreamResult(s, "application/vnd.ms-excel");
    }

Also tried to give name property

public ActionResult DownloadExcel() {
    string path = ExcelGenerationCode(fileName);
        Stream s = new FileStream(path, FileMode.Open, FileAccess.Read);
        return new FileStreamResult(s, "application/vnd.ms-excel")
        {
            FileDownloadName = "myexcelFILE1.xls"
        };
    }

getting below error when give filename parameter. enter image description here

i have plenty of space and memory.

Neo
  • 15,491
  • 59
  • 215
  • 405
  • 1
    Show the action method code that lets you download the file. – CodeCaster May 18 '14 at 08:57
  • ` public ActionResult DownloadExcel() {string path = ExcelGenerationCode(fileName); Stream s = new FileStream(path, FileMode.Open, FileAccess.Read); return new FileStreamResult(s, "application/vnd.ms-excel"); }` – Neo May 18 '14 at 09:18
  • 2
    After your edit, [this is the appropriate duplicate](http://stackoverflow.com/questions/5826649/returning-a-file-to-view-download-in-mvc): you can't set the filename for a `FileStreamResult`, use the [`File(Stream, String, String)` overload](http://msdn.microsoft.com/en-us/library/dd505200(v=vs.118).aspx). But you don't have to instantiate a stream manually, [`File(String, String, String)`](http://msdn.microsoft.com/en-us/library/dd492593(v=vs.118).aspx) does the same internally, so you'll have to use less code. – CodeCaster May 18 '14 at 09:28
  • 1
    I updated my answer. Is it working now? – Vajda Endre May 18 '14 at 09:47
  • @CodeCaster thanks but what do you mean "you'll have to use less code" Please tell me should I use File or FileStreamResult ? and what should be return type ? ActionResult or FileResult ? – Neo May 18 '14 at 10:44
  • 1
    Please try to search for errors. See [How to fix 'Microsoft Excel cannot open or save any more documents'](http://stackoverflow.com/questions/12415690/how-to-fix-microsoft-excel-cannot-open-or-save-any-more-documents). – CodeCaster May 18 '14 at 11:40
  • @CodeCaster if I only write `string path = DataSetsToExcel(dataSets, planRunName); Stream s = new FileStream(path, FileMode.Open, FileAccess.Read); return new FileStreamResult(s, "application/vnd.ms-excel");` everything working fine but as I mention in my question getting filename as some random GUID (F5CA852C-4F63-4F6D-82A9-45EB9E07FFBE.xls) and` if I specify the file name using property FileDownloadName` , I'm getting excel memory error :( – Neo May 18 '14 at 14:18
  • 1
    Does that also happen when you kill all Excel instances in your task manager before running this code? – CodeCaster May 18 '14 at 18:36
  • Ohh I did not realize that THANKS A LOT FRIEND :) but do you know how do I kill it in my code ? is it necessary to kill in my code ? – Neo May 19 '14 at 04:52

1 Answers1

12

You should return a new FileContentResult and your action must return FileResult instead of ActionResult for ex.:

public virtual FileResult DownloadFile(long fileId)
{
     string path = ExcelGenerationCode(fileName);
     return File(path, "application/vnd.ms-excel","donwload.xls");
}
Vajda Endre
  • 1,512
  • 1
  • 16
  • 26
  • updated my `ActionResult` code in `question above` please check – Neo May 18 '14 at 09:20
  • 1
    _"your action must be a FileResult"_ - not true, `FileResult` inherits `ActionResult`, so the latter will also work. – CodeCaster May 18 '14 at 10:06
  • working well but getting this error while opening "microsoft excel cannot open or save any more documents because there is not enough available memory" :/ – Neo May 18 '14 at 10:42
  • 1
    problem is i have already pass file name in path variable then also why it is not taking my file name :/ – Neo May 18 '14 at 14:11