ASP .NET(4.5)MVC byte []文件从数据库中检索困难

时间:2016-06-08 19:44:51

标签: c# asp.net asp.net-mvc asp.net-mvc-4

美好的一天,从我的数据库中检索一个简单的字节数组进行下载我遇到了很大的困难。我很清楚如何实现这一目标,据我所知,我的所有基础都已涵盖;但无法在相对路径中找到该文件。 DesignModel:

public class Message
{
    [Key]
    public int Id { get; set; }
    //...
    public byte[] Attachment { get; set; }
    public string ContentType { get; set; }
    public string ContentName { get; set; }
}

数据和元数据保存得非常好,所以我知道创建不是问题,只有我的检索有问题。
查看模特:

public class MessageAdd
{
    //..
    public HttpPostedFileBase Attachments { get; set; }
}

public class MessageBase : MessageAdd
{

    public MessageBase()
    {
        url = HttpContext.Current.Request.Url.GetLeftPart(System.UriPartial.Authority);
    }
    public int Id { get; set; }
    public string ContentType { get; set; }
    public string ContentName { get; set; }
    public byte[] Attachment { get; set; }

    private string url = "";
    public string AttachUrl
    {
        get
        {
            return string.Format("{0}/image/Message/Attachment{1}", url, Id);
        }
    }

}

消息控制器:

private Manager m = new Manager();
    // GET: Message
    [Authorize]
    public ActionResult Inbox()
    {
        return View(m.Inbox());
    }

Manager类收件箱的乐趣:

public IEnumerable<MessageBase> Inbox()
    {
        var messages = ds.Messages.Where(x => x.Recipient == uNm);

        return (messages == null) ? null : Mapper.Map<IEnumerable<MessageBase>>(messages);
    }

文件控制器:

private Manager m = new Manager();

    [Route("image/Message/Attachment/{id}")]
    public ActionResult GetMessageLogoById(int? id)
    {
        // Determine whether we can continue
        if (!id.HasValue) { return HttpNotFound(); }

        // Fetch the object, so that we can inspect its value
        var fetchedObject = m.GetMessageById(id.Value);

        if (fetchedObject == null)
        {
            return HttpNotFound();
        }
        else
        {
            // Return a file content result
            // Set the Content-Type header, and return the photo bytes
            return File(fetchedObject.Attachment, fetchedObject.ContentType, fetchedObject.ContentName);
        }

GetMessageByID(在经理中):

public MessageBase GetMessageById(int id)
    {
        var fetchedObject = Mapper.Map<MessageBase>(ds.Messages.Find(id));

        return (fetchedObject == null) ? null
            : fetchedObject;
    }

观点:

//..
        <th>
            Attachment
        </th>

        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
    //.. 

        @{
            if (item.ContentType != null)
            {
                if (item.ContentType.Contains("audio"))
                {
                    <td>
                        <audio controls>
                            <source src="@item.AttachUrl" type="@item.ContentType">
                            Audio playback not supported.
                        </audio> <br />
                        <a href='@item.AttachUrl' download> Download audio</a>
                    </td>
                }
                else if (item.ContentType.Contains("video"))
                {
                    <td>
                        <video width="300" height="280" controls>
                            <source src="@item.AttachUrl" type="@item.ContentType">
                            Video playback not supported.
                        </video> <br />
                        <a href='@item.AttachUrl' download> Download video</a>
                    </td>
                }
                else if (item.ContentType.Contains("image"))
                {
                    <td>
                        <a href='@item.AttachUrl' download>
                            <img src='@item.AttachUrl' alt="@item.ContentName" title="@item.ContentType" style="width: 100px;" />
                        </a>
                    </td>
                }
                else
                {
                    <td>
                        <a href='@item.AttachUrl' download>File - @item.ContentType</a>
                    </td>
                }
            }
            else
            {
                <td>No Attachment.</td>
            }
        }
    //..
    }@

我实际上已经在另一个项目中使用此方法获得了结果,当我比较两者时,我看不出我的逻辑有什么不同。任何灯棚都会受到赞赏。干杯

1 个答案:

答案 0 :(得分:0)

您的 AttachUrl 会返回错误的网址。 “{1}”之前没有“/”。

public string AttachUrl
{
    get
    {                                                   // here
        return string.Format("{0}/image/Message/Attachment/{1}", url, Id);
    }
}