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

时间:2019-06-21 16:04:50

标签: javascript asp.net-mvc model-view-controller knockout.js odata

我正在尝试使用敲除v3.2.0,webapi,odata下载文件,并在尝试将文件作为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。

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

1 个答案:

答案 0 :(得分:0)

所以我删除了[EnableQuery],它在IE和Chrome中都可以使用!