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

I'm getting a Error :"POST {URL} 415 (Unsupported Media Type)" error And cannot figure o

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744412808a4572966.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信