查询不能应用于 'System.Net.Http.StreamContent' 类型的响应内容。响应内容必须是一个 ObjectContent

Queries can not be applied to a response content of type 'System.Net.Http.StreamContent'. The response content must be an ObjectContent

我正在尝试使用 knockout v3.2.0、webapi、odata 下载文件,当我尝试 return 将文件作为 HttpResponseMessage 时出现此错误。

这是我的控制器代码:

 [EnableQuery]
public HttpResponseMessage GetAttachment([FromODataUri] int key)
{
    try
    { 
        DataAccess.Attachment a = db.Attachments.Where(x => x.AttachmentId == key).FirstOrDefault();               
        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
        MemoryStream memStream = new MemoryStream();
        memStream.Write(a.AttachmentData, 0, a.AttachmentData.Length);
        result.Content = new StreamContent(memStream);
        result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
        result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
        result.Content.Headers.ContentDisposition.FileName = a.AttachmentName;
        return result;
        //return Request.CreateResponse(HttpStatusCode.OK, result);

    }
    catch (Exception exception)
    {
        Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exception.Message);
        return null;
    }
}

这就是我尝试从 JavaScript 下载的方式:

    self.downloadDocument = function (attachmentId) {

        var serviceRequestUrl = dbhdd.buildUrl.buildSPContextUrl("/api/Attachments(" + 1 + ")");
        window.location.href = serviceRequestUrl;           

    };

这给了我这个错误——查询不能应用于 'System.Net.Http.StreamContent' 类型的响应内容。响应内容必须是ObjectContent。

我对此比较陌生。任何修复 this/alternate 方法的指导都将受到高度赞赏。

所以我删除了 [EnableQuery],它在 IE 和 Chrome 中都有效!