weixin_33724046 2016-07-06 20:00 采纳率: 0%
浏览 79

用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

  • 写回答

4条回答 默认 最新

  • weixin_33735676 2016-07-06 20:09
    关注

    Here is a way I accomplish downloading, hope that helps.

    $.ajax({
                url: sitePath + "Controller/DownloadFile?fileid=" + id,
                type: 'GET',
                success: function (filename) { //I return the filename in from the controller
                    frame = document.createElement("iframe");
                    frame.id = "iframe";
                    frame.style.display = 'none';
                    frame.src = '/FileDirectory?fileName=' + filename; //the path to the file
                    document.body.appendChild(frame);
                },
                cache: false,
                contentType: false,
                processData: false
            });
    
    评论

报告相同问题?