2016-11-09 1 views
0

Я использую код MVC C# для генерации файла excel, а затем загружаю, я использую EPPLUS, все работает отлично, за исключением Safari на Mac, когда я пытаюсь создайте файл, когда он загрузится, добавив .html в конец, например file.xlsx.html У меня нет идеи, как я могу это решить.Safari добавляет .html для загрузки xlsx с помощью epplus jquery.fileDownload.js

$.fileDownload(url, { 
     httpMethod: 'POST', 
     data: dtColumns, 
     successCallback: function() { 
      //code 
     }, 
     failCallback: function() { 
      //code 

     } 
    }); 

общественный поток ExportDataset (IEnumerable набор данных, IList columnsToExport, шаблон строки) { // Группировка работ по типам Анон, поэтому мы можем сгруппировать экспорт в свои собственные таблицы вар группировки = dataset.GroupBy (I = > i.GetType());

 using (var package = new ExcelPackage(new FileInfo(GetTemplateFilePath(template)), true)) 
     { 
      var ws = package.Workbook.Worksheets[1]; 

      if (groupings.Any()) 
      { 
       // add each "anon type matched grouping" 
       foreach (var grouping in groupings) 
       { 
        // because of EPP inheritance bug of T, we can just use dataTable 
        var dataTable = new DataTable(grouping.Key.Name); 
        var properties = grouping.Key.GetProperties(); // Get anon type Properties 
        var columns = columnsToExport.OrderBy(x => x.Position); 

        foreach (var property in columns.Where(column => column.IsVisible).SelectMany(column => properties.Where(property => property.Name.Equals(column.Name)))) 
        { 
         dataTable.Columns.Add(property.Name); 
        } 

        foreach (var item in grouping.ToList()) 
        { 
         var dataRow = dataTable.NewRow(); 

         foreach (var p in columns.Where(column => column.IsVisible).SelectMany(column => properties.Where(property => property.Name.Equals(column.Name)))) 
         { 
          dataRow[p.Name] = p.GetValue(item); 
         } 

         dataTable.Rows.Add(dataRow); 
        } 

        ws.Cells[1, 1].LoadFromDataTable(dataTable, PrintHeaders: true); 
        ws.Cells.AutoFitColumns(); 
       } 
      } 
      else 
      { 
       // This case is when there is no data on the table to load the Excel so we create an empty sheet but we add the headers 
       var datasetStructure = dataset.GetType().GetGenericArguments()[0]; 

       // because of EPP inheritance bug of T, we can just use dataTable 
       var dataTable = new DataTable(datasetStructure.Name); 
       var properties = datasetStructure.GetProperties(); // Get anon type Properties 

       foreach (var property in properties) 
       { 
        dataTable.Columns.Add(property.Name); 
       } 

       ws.Cells[1, 1].LoadFromDataTable(dataTable, PrintHeaders: true); 
       ws.Cells.AutoFitColumns(); 
      } 

      var fileStream = new MemoryStream(); 
      package.SaveAs(fileStream); 

      return fileStream; 
     } 
    } 

на контроллере:

возврата нового CustomFileResult (FileStream, Filename);

общественный класс CustomFileResult: IHttpActionResult { private readonly Stream _fileContent; частная строка readonly _fileName;

public CustomFileResult(Stream fileContent, string fileName) 
    { 
     _fileContent = fileContent; 
     _fileName = fileName; 
    } 

    public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken) 
    { 
     return Task.Run(() => 
     { 
      var response = new HttpResponseMessage(); 
      var fileDownloadFail = false; 

      if (_fileContent == null || _fileContent.Length == 0) 
      { 
       response.StatusCode = HttpStatusCode.InternalServerError; 
       fileDownloadFail = true; 

      } 
      else 
      { 
       response.StatusCode = HttpStatusCode.OK; 
       _fileContent.Position = 0; 
       response.Content = new StreamContent(_fileContent); 

       response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") 
       { 
        FileName = _fileName, 
       }; 
      } 

      //Required for plugin jquery.fileDownload.js 

      var cookie = new CookieHeaderValue("fileDownload", "true") { Path = "/" }; 

      response.Headers.AddCookies(new CookieHeaderValue[] { cookie }); 

      return response; 

     }, cancellationToken); 
    } 
} 
+0

Решение для этого - добавить тип контента в ответ: – jruiz

ответ

1

Решение этого является добавить CONTENTTYPE, чтобы преуспеть в ответ, по умолчанию текст/HTML, поэтому сафари для причины дона; т понять ответ файл Excel, поэтому добавьте следующую строку:

response.Content.Headers.ContentType = new MediaTypeHeaderValue ("application/vnd.ms-excel");

проблема была решена.