1

Hi I am trying to download file in mvc4. Below is my code to download the file. I am making ajax request to get file. My controller code looks like fine. variable result will get required bytes. I am having trouble with success gfunction in Download function in jquery. control will not come to success function. Even my alert is not working. Am i missing anything in the above code? Can someone please suggest me?

 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)
    }
Niranjan Godbole
  • 2,135
  • 7
  • 43
  • 90

1 Answers1

0

You should not download the file using ajax request, if you want to preview the file, then update the code that opens the dialog and set the FileName to the URL for the file itself

function Download(pClientid) {
    var fileUrl = '@Url.Action("Download", "TestRendering",new {ClientID="_X_"})';
    fileUrl = fileUrl.replace ("_X_",fileName)
    ShowFiepopup(fileUrl)
    }
Haitham Shaddad
  • 4,336
  • 2
  • 14
  • 19
  • Thank you. when i put alert fileUrl will hold /testrendering/download/2392. In the above code my control is not going back to controller simply it will display filurl. – Niranjan Godbole Sep 29 '16 at 13:52
  • see the answer for this question http://stackoverflow.com/questions/4853898/display-pdf-within-web-browser basically you will replace the src attribute with the url for the file – Haitham Shaddad Sep 29 '16 at 16:07
  • Yes i agree. Lets say my url is F:\UploadedFile\1007\1007_20160930093052693_2745c7a9-3a60-4d78-b2b2-5b0b885a3d7b.pdf But when i host this application and when i access the application from other systems How it will work? – Niranjan Godbole Sep 30 '16 at 04:14
  • In this case your controller action will convert this URL to be: http://yourserver/TestRendering/Download/1232 – Haitham Shaddad Sep 30 '16 at 07:57