2016-11-14 61 views
0

我需要設置創建一個頁面,我有按鈕,點擊後,它應該重定向到註冊頁面,然後下載PDF文件。所以我在Umbraco中創建了一個具有文件上傳字段的文檔類型,並且我通過它上傳了一個文件。在它的模板上,我添加了一個宏,它具有註冊頁面的局部視圖。完成註冊後,這個pdf文件應該自動下載。我可以下載保存在媒體中的Umbraco pdf文件

我的問題是,我上傳的文件沒有顯示在媒體庫中。但Url如下所示:/media/1051/filname.pdf。

enter image description here

正在此URL中的控制器。但無法使用它的ID獲取文件。

[HttpPost] 
     public HttpResponseMessage DownloadFile([FromBody] DownloadEBookViewModel model) 
     { 
      int id = Convert.ToInt32(model.Url.Split('/')[2]); 
      var media = Umbraco.Media(id).Url; 


      if (!File.Exists(media)) 
       throw new HttpResponseException(HttpStatusCode.NotFound); 

      HttpResponseMessage Response = new HttpResponseMessage(HttpStatusCode.OK); 


      byte[] fileData = File.ReadAllBytes(media); 

      if (fileData == null) 
       throw new HttpResponseException(HttpStatusCode.NotFound); 

      Response.Content = new ByteArrayContent(fileData); 
      Response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); 

      return Response; 
     } 

有人請幫忙。謝謝

回答

0

當代碼中的一把umbraco幫手後面時,我會建議使用類型變量用於獲取項目

var media = Umbraco.TypedMedia(id).Url;

這會給你的智能感知的強類型模型

要獲取物理文件從媒體對象,你可能會想打電話

byte[] fileData = File.ReadAllBytes(media.getPropertyValue("umbracoFile"));

代替:

byte[] fileData = File.ReadAllBytes(media);

(代碼是未測試的)

+0

感謝您的答覆。它真的幫助我 –

+0

不客氣:) – Mark

相關問題