无法在mvc4和jquery中返回文件

时间:2016-09-29 13:19:17

标签: jquery asp.net-mvc asp.net-mvc-4

您好我正在尝试在mvc4中下载文件。下面是我下载文件的代码。我正在制作ajax请求来获取文件。我的控制器代码看起来很好。变量结果将获得所需的字节。我在jquery中的下载功能中遇到了成功gfunction的问题。控制不会成功的功能。即使我的警报也无效。我在上面的代码中遗漏了什么吗?有人可以建议我吗?

 public ActionResult Download(int? ClientId)
        {
            service.Service objService = new service.Service();
            DashboardBAL objdb = new DashboardBAL();
            string filepath = objdb.GetFilepath(ClientId.GetValueOrDefault(0));
            string folderName = @"F:\UploadedFile";
            string serverMappath = (folderName);
             byte[] result = objService.DownloadFileFromDMS(filepath);
            string contentType = MimeMapping.GetMimeMapping(filepath);
            string FileName = Path.GetFileName(filepath);
            var cd = new System.Net.Mime.ContentDisposition
            {
                FileName = Path.GetFileName(filepath),
                Inline = true,
            };
            if (!System.IO.Directory.Exists(serverMappath))
            {
                System.IO.Directory.CreateDirectory(serverMappath);
            }
            string strdocPath = serverMappath + @"\" + FileName;
            if (result != null)
            {
                FileStream objfilestream = new FileStream(strdocPath, FileMode.Create, FileAccess.Write);
                objfilestream.Write(result, 0, result.Length);
                objfilestream.Close();
            }
            return File(result, System.Net.Mime.MediaTypeNames.Application.Octet, FileName);
        }


function Download(pClientid) {
        $.ajax(
        {
            type: "GET",
            cache: false,
            data: { ClientId: pClientid },
            contentType: 'application/json; charset=utf-8',
            dataType: "json",
            cache: false,
            url: '@Url.Action("Download", "TestRendering")',
           // url: '/TestRendering/Download',
            headers: {
                'VerificationToken': forgeryId
            },
            success: function (data) {
                alert(1);
                ShowFiepopup(data);
            }
        });
    }


function ShowFiepopup(FileName) {
        $("#dialog").dialog({
            modal: true,
            title: "Preview of " + FileName,
            width: 850,
            height: 600,
            buttons: {
                Close: function () {
                    $(this).dialog('close');
                }
            },

            open: function () {
                var object = "<object data=\"{FileName}\" type=\"application/pdf\" Zoom=\"100%\" width=\"800px\" height=\"600px\">";
                object += "If you are unable to view file, you can download from <a href=\"{FileName}\">here</a>";
                object += " or download <a target = \"_blank\" href = \"http://get.adobe.com/reader/\">Adobe PDF Reader</a> to view the file.";
                object += "</object>";
                object = object.replace(/{FileName}/g, "../UploadedFile/" + FileName);
                $("#dialog").html(object);

            }
        });
    };


function Download(pClientid) {
        var fileUrl = '@Url.Action("Download", "TestRendering",new {ClientID= "pClientid" })';
        alert(fileUrl);
        fileUrl = fileUrl.replace("_X_", fileName);
        ShowFiepopup(fileUrl)
    }

1 个答案:

答案 0 :(得分:0)

如果要预览文件,则不应使用ajax请求下载文件,然后更新打开对话框的代码并将FileName设置为 文件本身的URL

function Download(pClientid) {
    var fileUrl = '@Url.Action("Download", "TestRendering",new {ClientID="_X_"})';
    fileUrl = fileUrl.replace ("_X_",fileName)
    ShowFiepopup(fileUrl)
    }