1

我正在尝试做什么 - 当用户单击链接时,从 RavenDB 获取附件并自动为用户打开它。

单击链接时 - 我通过视图/ajax 将附件的附件 ID(已保存在 RavenDB 中)传递给控制器​​方法。一旦进入控制器方法,我想获取附件并为用户显示/打开附件。

看法 :

<a href onclick="GetAttachment('<%= Model.Id %>');"> See attachment </a>

阿贾克斯/JS

function GetAttachment(id) {   

$.ajax({
    type: 'POST',
    url: '/ControllerName/GetMethod',
    data: id,
    contentType: 'application/json; charset=utf-8',
    success: function (msg) {
        if (msg.Success) {               
        }
        else {               
        }
     }       
  });
}

控制器 :

    public string GetMethod(string id)
    {          
        var dbCommands = session.Advanced.DatabaseCommands;
        var attachment = dbCommands.GetAttachment(id);
       //Here - How do I use above lines of code to get hold of the 
       // attachment and open it for the user.
    }

感谢您的帮助。

4

2 回答 2

3

The RavenDB Attachment class has a Data property of type Func<Stream> which is a thunk to the byte stream for the attachment. The stream is what you need to return in your MVC controller:

public FileResult GetMethod(string id)
{          
    var dbCommands = session.Advanced.DatabaseCommands;
    var attachment = dbCommands.GetAttachment(id);
    return File(attachment.Data(), "fileName");
}
于 2012-08-02T05:08:19.503 回答
1

像这样的东西:控制器:

public FileResult GetMethod(string id)
{          
    var name = "filename.extension";
    var dbCommands = session.Advanced.DatabaseCommands;
    var attachment = dbCommands.GetAttachment(id);
    var stream = new MemoryStream(attachment.Data);
    return new FileStreamResult(stream, name);
}

看法:

@Html.ActionLink("见附件", "GetMethod", "ControllerName", new {id = ModelId}, null)

您需要从数据库中获取文件的 MemoryStream 或将文件传输到 MemoryStream

于 2012-08-01T14:52:16.010 回答