【问题标题】:Queries can not be applied to a response content of type 'System.Net.Http.StreamContent'. The response content must be an ObjectContent查询不能应用于“System.Net.Http.StreamContent”类型的响应内容。响应内容必须是 ObjectContent
【发布时间】:2019-11-04 12:17:58
【问题描述】:

我正在尝试使用 Knockout 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。

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2017-05-07
      • 2014-12-07
      • 2011-01-21
      • 2010-09-28
      • 2019-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-17
      • 1970-01-01
      相关资源
      最近更新 更多