Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Angular、jquery、将文件发布到c#web api、错误上载INFG未报告的媒体类型_Javascript_Jquery_Angularjs_Image_Asp.net Web Api - Fatal编程技术网

Javascript Angular、jquery、将文件发布到c#web api、错误上载INFG未报告的媒体类型

Javascript Angular、jquery、将文件发布到c#web api、错误上载INFG未报告的媒体类型,javascript,jquery,angularjs,image,asp.net-web-api,Javascript,Jquery,Angularjs,Image,Asp.net Web Api,您好,我正在尝试将带有图像的post请求发送到我的web api,但我返回了那个错误:“上载未移植的媒体类型”错误。我不知道如何避免这个错误。部分代码: 美联社 .html文件 <input type="file" name="file" onchange="angular.element(this).scope().uploadFile(this)" /> 现在我的webapi中出现了错误;)所以请求通过;)在webapi中,我尝试执行以下操作: [HttpPost]

您好,我正在尝试将带有图像的post请求发送到我的web api,但我返回了那个错误:“上载未移植的媒体类型”错误。我不知道如何避免这个错误。部分代码: 美联社

.html文件

<input type="file" name="file" onchange="angular.element(this).scope().uploadFile(this)" />
现在我的webapi中出现了错误;)所以请求通过;)在webapi中,我尝试执行以下操作:

    [HttpPost]
    public string Post(HttpPostedFile profileImage)
    {
        string[] extensions = { ".jpg", ".jpeg", ".gif", ".bmp", ".png" };
        if (!extensions.Any(x => x.Equals(Path.GetExtension(profileImage.FileName.ToLower()), StringComparison.OrdinalIgnoreCase)))
        {
            throw new HttpResponseException( HttpStatusCode.BadRequest);
        }

        // Other code goes here

        return "/path/to/image.png";
    }

但参数HttpPostedFile profileImage始终为空。你知道我应该怎么写这篇文章吗?

内容类型:undefined
可能不应该在那里。此外,请求似乎正在离开您的页面,因为警报位于ajax方法的错误部分,除非收到错误响应,否则不会触发警报。是的,我应该在警报中键入我得到错误。那么WebAPI有什么问题呢?或者我应该选择哪种内容类型发布图像?可能是web api的问题。尝试从传递到
.ajax()
的对象中删除
标题
属性,然后查看更新后发生了什么,但是现在我的webapi中的http ost方法出现了问题。我在编辑下面写了一些东西。尝试将
contentType:false
processData:false
添加到ajax对象中。那可能会解决它
$scope.uploadFile = function (element) {
    var data = new FormData();
    data.append('file', $(element)[0].files[0]);
    jQuery.ajax({
        url: 'http://localhost:56392/api/Image',
        type: 'post',
        data: data,



        processData: false,
        success: function (response) {
            console.log(response);
        },
        error: function (jqXHR, textStatus, errorMessage) {
            alert('Error uploading: ' + errorMessage);
        }
    });
};
    [HttpPost]
    public string Post(HttpPostedFile profileImage)
    {
        string[] extensions = { ".jpg", ".jpeg", ".gif", ".bmp", ".png" };
        if (!extensions.Any(x => x.Equals(Path.GetExtension(profileImage.FileName.ToLower()), StringComparison.OrdinalIgnoreCase)))
        {
            throw new HttpResponseException( HttpStatusCode.BadRequest);
        }

        // Other code goes here

        return "/path/to/image.png";
    }