2013-01-06 14 views
5

Sto inviando l'oggetto JSON tramite il seguente codice. Controller che restituisce valori in formato CSV che dovrebbero essere forniti di prompt per aprire o salvare come file CSV. Non riesco a capire che cosa esattamente codice dovrebbe essere scrivere nel successo: la funzione (risultato)Come gestire il tipo di ritorno FileStream nel post .ajax?

Link for Export

Html.ActionLink("Export", "", "", null, new { @onclick = "return exportData();"}) 

function exportData() { 

var searchViewModel = getExLogSearchModelData(); 
var path = getAbsolutePath("ExclusionLog", "ExportData"); 
$.ajax({ 
    url: path, 
    cache: false, 
    contentType: 'application/json; charset=utf-8', 
    dataType: 'html', 
    type: 'POST', 
    data: JSON.stringify(searchViewModel), 
    success: function (result) { 
    }, 
    error: function (error) { 
     $("#voidcontainer").html("Error"); 
    } 
}); 

}

controller ActionResult

public ActionResult ExportData(ExclusionLogSearchViewModel postSearchViewModel) 
    { 
     try 
     { 
      IEnumerable<ExclusionLogViewModel> exclusionLogData = null; 
      exclusionLogcData = this._workerService.GetExclusionLogData(postSearchViewModel); 

      return CSVFile(exclusionLogMembershipSyncData.GetStream<ExclusionLogViewModel>(), "ExclusionLogTables.Csv"); 
     } 
     catch (Exception ex) 
     { 
       throw ex; 
     } 
     return null; 
    } 

Potete per favore suggerire come fare questo?

risposta

3

ho colpito lo stesso problema e non ho trovato un modo per scaricare file usando $.ajax ma ho trovato un ottimo libreria JavaScript che fornisce un comportamento simile:

https://github.com/johnculviner/jquery.fileDownload

Hai solo bisogno di invocare qualcosa di simile:

$.fileDownload(path, { 
    httpMethod: 'POST', 
    data: JSON.stringify(searchViewModel), 
    success: function (result) { 
    }, 
    error: function (error) { 
     $("#voidcontainer").html("Error"); 
    } 
}); 

E ricordarsi di creare cookie nel controller. In src/Common c'è un filtro di azione adatto che lo fa per te.