最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - 415 Unsupported Media Type Using JQueryFormData and Web Api - Stack Overflow

programmeradmin3浏览0评论

I'm getting a Error :

"POST {URL} 415 (Unsupported Media Type)" error 

And cannot figure out why it's happening.

I'm trying to upload an excel file in JQuery using FormData.

Here is the code:

var formdata = new FormData();
var file = input.get(0).files[0];
formdata.append('content', file);

var url = "/Phrase/Import/" + $('.exportPanel #Language').val()

var ajax = $.ajax({
    url: url,
    type: "POST",
    data: formdata,
    processData: false,
    contentType: false,
    success: function (data, textStatus, jqXHR) {
        //Do something.
    },
    error: function (jqXHR, textStatus, errorThrown) {
        //Do something.
    }
});

Here is the controller code:

[Route("Import/{languageID}")]
[HttpPost]
public void ImportPhrases([FromUri]int languageID, [FromBody]Stream content)
{
    _service.ImportPhrases(content, languageID);          
}

I noticed that, according to Fiddler, the content type of the request is different to that of the response (not sure if this makes a difference?).

Request: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Response: application/json; charset=utf-8

The JQuery above is being used in a different part of the system, but uses WCF instead of Web API (am in the process of changing from WCF to MVC/Web API), again I'm not sure if this makes a difference?

Can anyone tell me what I'm doing wrong please?

Many thanks.

I'm getting a Error :

"POST {URL} 415 (Unsupported Media Type)" error 

And cannot figure out why it's happening.

I'm trying to upload an excel file in JQuery using FormData.

Here is the code:

var formdata = new FormData();
var file = input.get(0).files[0];
formdata.append('content', file);

var url = "/Phrase/Import/" + $('.exportPanel #Language').val()

var ajax = $.ajax({
    url: url,
    type: "POST",
    data: formdata,
    processData: false,
    contentType: false,
    success: function (data, textStatus, jqXHR) {
        //Do something.
    },
    error: function (jqXHR, textStatus, errorThrown) {
        //Do something.
    }
});

Here is the controller code:

[Route("Import/{languageID}")]
[HttpPost]
public void ImportPhrases([FromUri]int languageID, [FromBody]Stream content)
{
    _service.ImportPhrases(content, languageID);          
}

I noticed that, according to Fiddler, the content type of the request is different to that of the response (not sure if this makes a difference?).

Request: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Response: application/json; charset=utf-8

The JQuery above is being used in a different part of the system, but uses WCF instead of Web API (am in the process of changing from WCF to MVC/Web API), again I'm not sure if this makes a difference?

Can anyone tell me what I'm doing wrong please?

Many thanks.

Share Improve this question edited Jan 12, 2015 at 12:29 Dharmesh Porwal 1,4062 gold badges12 silver badges22 bronze badges asked Jan 12, 2015 at 11:56 MattplusMattplus 571 silver badge7 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 5

415 Unsupported Media Type

The request entity has a media type which the server or resource does not support. For example, the client uploads an image as image/svg+xml, but the server requires that images use a different format.

contentType does matter - it tells the server what you are uploading. In this instance, you have set the value to false. The server cannot recognise this, so it returns false. If you do not need a specific content type, you should remove the contentType line to allow the jQuery default to kick in.

发布评论

评论列表(0)

  1. 暂无评论