IFL IFL - 4 months ago 13
Ajax Question

Download a file with mvc

I need to allow the user on my site to download a file (xml File)

I tried

public FileResult DownloadFile(string fileid)
{
if (!string.IsNullOrEmpty(fileid))
{
byte[] fileBytes = Encoding.ASCII.GetBytes(FileData);
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet,FileName + ".bccx");
}
return null;
}


ajax:

function downloadFile(id) {
$.ajax({
url: sitePath + "Controller/DownloadFile?fileid=" + id,
type: 'post',
asysnc: false
})
.done(function () {

});
}


but nothing is downloaded

Answer

Does it have to be done using ajax? Maybe you could open another window with the file generation address and let the browser do the job:

function downloadFile(id) {

    window.open(sitePath + "Controller/DownloadFile?fileid=" + id, '_blank');

}
Comments