You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Custom Vision Predict API遇BadRequestImageFormat错误求助

Fixing BadRequestImageFormat Error in Custom Vision Service

Hey there, let's break down why you're hitting that BadRequestImageFormat error and how to fix it. I've spotted a couple of key issues in your code that are likely causing the problem:

1. Missing name attribute on your file input

Custom Vision's prediction API expects the uploaded image to be sent under a specific form field name. Your current <input type="file"/> doesn't have a name attribute, so the service can't properly identify the image data you're sending.

Update your HTML form to add the required name attribute (the service looks for file as the field name):

<form id="imageform" method="POST" enctype="multipart/form-data">
  <input type="file" name="file"/>
</form>

2. Incorrect contentType setting in your AJAX call

When using FormData with jQuery's ajax() method, you shouldn't manually set contentType: "multipart/form-data". FormData automatically generates the correct content type header, including the unique boundary string that the server needs to parse the multipart data. Manually setting this value overrides the auto-generated header, leading to parsing errors.

Fix your AJAX configuration by setting contentType: false:

var data = new FormData(document.getElementById('imageform'));
var url = "https://southcentralus.api.cognitive.microsoft.com/customvision/v1.0/Prediction/my-key/image";

$.ajax({
  url : url,
  data : data,
  processData : false,
  contentType : false, // Let FormData handle the content type automatically
  headers : {
    'Prediction-key' : 'your-actual-prediction-key' // Make sure this is your valid key
  },
  type : 'POST',
  success : function(response) {
    var result = response["Predictions"];
    buildResult(result);
  },
  error : function(request, status, error) {
    console.log("Error details:", request.responseText); // Add this to debug future issues
  }
});

Additional Checks to Rule Out Other Issues

  • Verify your image is in a supported format: Custom Vision accepts JPEG, PNG, GIF, and BMP files.
  • Ensure the image file isn't corrupted—try opening it in an image viewer to confirm.
  • Double-check that your Prediction key and API endpoint (southcentralus) match the region and credentials for your Custom Vision project.

内容的提问来源于stack exchange,提问作者gnogun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:41:55