如何在MVC4应用程序的jquery弹出窗口中显示下载的文件?

时间:2016-09-26 07:33:20

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

在我的一个mvc4应用程序中,我上传文件并将其保存在F:\ uploadedfile \ filename.pdf路径中。我也托管了应用程序本地IIS。下面是我下载文件的动作方法。

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 = ("~/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 Json(FileName, JsonRequestBehavior.AllowGet);

        }

这是jquery代码。

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);
            }
        });
    };

我无法在上面的代码中下载文件。我只返回文件名。我需要返回字节并将其传递给Jquery代码吗?在下面的代码行中如何将字节绑定到对象?

  object = object.replace(/{FileName}/g, "../UploadedFile/" + fileName);

有人能告诉我吗?提前谢谢。

我改变如下。

  System.Uri uri1 = new Uri(strdocPath);
            System.Uri uri2 = new Uri(serverMappath);
            Uri relativeUri = uri2.MakeRelativeUri(uri1);
            string FileName1 = Uri.UnescapeDataString(relativeUri.ToString());

            return Json(FileName1, JsonRequestBehavior.AllowGet);

现在我应该在下面的代码行中替换什么?

   object = object.replace(/{FileName}/g, "F:/UploadedFile/" + FileName);

0 个答案:

没有答案