2016-09-29 58 views
1

您好我正在嘗試下載mvc4中的文件。以下是我的代碼來下載文件。我正在做一個Ajax請求來獲取文件。我的控制器代碼看起來很好。變量結果將得到所需的字節。我在jquery的Download函數中遇到了成功函數的問題。控制不會成功的功能。即使我的警報不起作用。我是否缺少上述代碼中的任何內容?有人可以建議我嗎?無法在mvc4和jquery中返回文件

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) 
    } 
+0

是什麼問題..是否會引發任何錯誤的操作方法? –

+0

不,它沒有返回任何東西。我有Jquery功能,下載。在成功塊我沒有得到任何東西。即使我提出了一個警報,但它也不起作用 –

+0

您是否在調試器控制檯中查看了網絡選項卡?您回覆的回覆是什麼?\ – arjabbar

回答

0

你不應該使用Ajax請求下載文件,如果你想預覽的文件,然後更新打開的對話框中的代碼,並設置FileName到 URL的文件本身

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

謝謝。當我把警報fileUrl將舉行/ testrendering /下載/ 2392。在上面的代碼中,我的控件不會回到控制器,只是它會顯示filurl。 –

+0

看到這個問題的答案http://stackoverflow.com/questions/4853898/display-pdf-within-web-browser 基本上,你會用文件的url替換src屬性 –

+0

是的,我同意。可以說我的網址是F:\ UploadedFile \ 1007 \ 1007_20160930093052693_2745c7a9-3a60-4d78-b2b2-5b0b885a3d7b.pdf但是,當我主持這個應用程序,當我從其他系統訪問應用程序它將如何工作? –