我需要设置创建一个页面,我有一个按钮,点击它,它应该重定向到一个注册页面,然后下载一个PDF文件。所以我在Umbraco中创建了一个文件类型,它有一个文件上传字段,我通过它上传了一个文件。在其模板上,我添加了一个宏,它具有注册页面的部分视图。完成注册后,此pdf文件应自动下载。
我的问题是,我上传的文件没有显示在媒体库中。但是网址如下:/media/1051/filname.pdf。
我在控制器中得到这个网址。但无法使用其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;
这将为您提供具有intellisense的强类型模型
要从媒体对象获取物理文件,您可能需要调用它
byte[] fileData = File.ReadAllBytes(media.getPropertyValue("umbracoFile"));
代替:
byte[] fileData = File.ReadAllBytes(media);
(代码未经测试)