【问题标题】:415 (Unsupported Media Type) Error415(不支持的媒体类型)错误
【发布时间】:2016-09-09 07:13:21
【问题描述】:

在我的 MVC 项目中,我使用 XmlHttpRequest 向 Web API 发出 POST 请求。

我以 JSON 格式发送一组文档的路由,并期望从服务器获取一个 Zip 文件 (ArrayBuffer)。

self.zipDocs = function (docs, callback) {
    var xhr = new XMLHttpRequest();

    xhr.onreadystatechange = function () {//Call a function when the state changes.
        if (xhr.readyState == 4 && xhr.status == 200) {
            alert(xhr.responseBody);
        }
    }
    xhr.open("POST", '../API/documents/zip', true);
    xhr.setRequestHeader("Content-type", "application/json");
    xhr.responseType = "arraybuffer";
    console.log(docs);
    xhr.send(docs);

    var arraybuffer = xhr.response;
    var blob = new Blob([arraybuffer], { type: "application/zip" });
    saveAs(blob, "example.zip");
}

还有我在 WebAPI 上的 ZipDocs 函数(使用 DotNetZip 库):

[HttpPost]
    [Route("documents/zip")]
    public HttpResponseMessage ZipDocs([FromBody] string[] docs)
    {

    using (var zipFile = new ZipFile())
    {
        zipFile.AddFiles(docs, false, "");
        return ZipContentResult(zipFile);
    }
}

protected HttpResponseMessage ZipContentResult(ZipFile zipFile)
{
    // inspired from http://stackoverflow.com/a/16171977/92756
    var pushStreamContent = new PushStreamContent((stream, content, context) =>
    {
       zipFile.Save(stream);
        stream.Close(); // After save we close the stream to signal that we are done writing.
    }, "application/zip");

    return new HttpResponseMessage(HttpStatusCode.OK) { Content = pushStreamContent };
}

但是我从服务器得到的响应是:

POST http://localhost:1234/MyProject/API/documents/zip 415 (Unsupported Media Type)

为什么会发生这种情况,我该如何解决?

【问题讨论】:

    标签: javascript c# asp.net-web-api xmlhttprequest zip


    【解决方案1】:

    感谢@David Duponchel 我使用了jquery.binarytransport.js 库,我将数据作为 JSON 发送到 API,并以二进制形式取回 Zip 文件。

    这是我的 JavaScript ZipDocs 函数:

    self.zipDocs = function (docs, callback) {
        $.ajax({
            url: "../API/documents/zip",
            type: "POST",
            contentType: "application/json",
            dataType: "binary",
            data: docs,
            processData: false,
            success: function (blob) {
                saveAs(blob, "ZippedDocuments.zip");
                callback("Success");
            },
            error: function (data) {
                callback("Error");
            }
        });
    }
    

    API 的代码保持不变。

    效果很好。

    【讨论】:

      【解决方案2】:

      基于this post

      你可以试试

      xhr.setRequestHeader("Accept", "application/json");
      

      而且您的代码在

      上缺少分号
      xhr.setRequestHeader("Content-type", "application/json")
      

      【讨论】:

      • 服务器端是否允许使用这些 mime 类型?
      • 你试过 utf-8 吗? xhr.setRequestProperty("Content-Type", "application/json; charset=utf8")
      猜你喜欢
      • 2018-10-13
      • 2018-07-25
      • 2017-08-28
      • 2021-10-12
      • 2017-10-23
      • 2014-10-02
      • 2014-06-21
      • 2016-09-10
      • 2020-06-26
      相关资源
      最近更新 更多