我可以下载保存在 Umbraco 媒体中的 pdf 文件吗

Can i download a pdf file saved in media in Umbraco

我需要设置创建一个页面,上面有按钮,点击后,它应该重定向到注册页面,然后下载 pdf 文件。所以我在 Umbraco 中创建了一个文档类型,它有一个文件上传字段,我通过它上传了一个文件。在其模板上,我添加了一个宏,该宏具有注册页面的部分视图。完成注册后,此pdf文件应该会自动下载。

我的问题是,我上传的文件没有显示在媒体库中。但是 Url 如下: /media/1051/filname.pdf .

我正在控制器中获取此 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;
        }

有人请帮助。谢谢

在代码隐藏中使用 Umbraco 助手时,我建议使用类型化变体来获取项目

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

这将为您提供一个带有智能感知的强类型模型

要从您可能想要调用的媒体对象中获取物理文件

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

而不是:

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

(代码未经测试)