{"record":{"id":"13bd70c0aca63cf3","repo":"iflytek/astron-agent","slug":"8001-invalid-file-format-please-upload-a-png-or-jpg-image","errorCode":"8001","errorMessage":"Invalid file format, please upload a png or jpg image","messagePattern":"Invalid file format, please upload a png or jpg image","errorType":"error_code","errorClass":"BusinessException","httpStatus":null,"severity":"warning","filePath":"console/backend/toolkit/src/main/java/com/iflytek/astron/console/toolkit/controller/common/ImageController.java","lineNumber":59,"sourceCode":"     * </p>\n     *\n     * @param file multipart file to upload; must not be {@code null}\n     * @return {@link ApiResult} wrapping a JSON object containing:\n     *         <ul>\n     *         <li>{@code s3Key} - object key in S3</li>\n     *         <li>{@code downloadLink} - accessible download URL</li>\n     *         </ul>\n     * @throws BusinessException if the file name is invalid, file suffix is unsupported, or upload\n     *         fails\n     */\n    @PostMapping(\"/upload\")\n    public ApiResult<JSONObject> upload(@RequestParam(\"file\") MultipartFile file) {\n        // File suffix validation\n        List<String> allowedSuffixes = Arrays.asList(\"png\", \"jpg\", \"jpeg\");\n        String fileName = file.getOriginalFilename();\n\n        if (fileName == null || !fileName.contains(\".\")) {\n            throw new BusinessException(ResponseEnum.RESPONSE_FAILED, \"Invalid file format, please upload a png or jpg image\");\n        }\n\n        String suffix = fileName.substring(fileName.lastIndexOf('.') + 1).toLowerCase();\n        if (!allowedSuffixes.contains(suffix)) {\n            throw new BusinessException(ResponseEnum.RESPONSE_FAILED, \"Invalid file format, please upload a png or jpg image\");\n        }\n\n        String s3Key = imageService.upload(file);\n        JSONObject res = new JSONObject();\n        // Generate unique file name\n        res.put(\"s3Key\", s3Key);\n        res.put(\"downloadLink\", s3UtilClient.getS3Url(s3Key));\n        return ApiResult.success(res);\n    }\n}\n","sourceCodeStart":41,"sourceCodeEnd":75,"githubUrl":"https://github.com/iflytek/astron-agent/blob/5e758547a83371a5a4b29dadf4ac03e8dd527635/console/backend/toolkit/src/main/java/com/iflytek/astron/console/toolkit/controller/common/ImageController.java#L41-L75","documentation":"ImageController.upload validates the uploaded file's original filename before storing it. If the filename is null or contains no '.', it throws BusinessException(ResponseEnum.RESPONSE_FAILED, code 8001) with message 'Invalid file format, please upload a png or jpg image'. The suffix check cannot run without a dot-separated extension, so the request is rejected up front.","triggerScenarios":"POSTing a multipart file to the image upload endpoint where file.getOriginalFilename() is null (no filename part) or has no '.' in it — e.g. a filename like 'image' with no extension.","commonSituations":"Clients uploading files without extensions; curl uploads using @file with a bare name; programmatic clients constructing MultipartFile wrappers with null filenames; drag-and-drop uploads of extensionless files; some browsers stripping extensions for certain MIME types.","solutions":["Rename the file client-side to include a valid .png/.jpg/.jpeg extension before uploading.","Verify the multipart form field is named 'file' and includes the filename in Content-Disposition.","Send Content-Type multipart/form-data so Spring populates getOriginalFilename() correctly.","If the file is genuinely an image without extension, add the correct extension before upload."],"exampleFix":"// before\nconst fd = new FormData(); fd.append('file', blob, 'screenshot'); // no extension\n// after\nconst fd = new FormData(); fd.append('file', blob, 'screenshot.png');","handlingStrategy":"validation","validationCode":"const name = file?.name || '';\nif (!name.includes('.')) { alert('Please choose a png or jpg image'); return; }","typeGuard":"function hasImageExtension(file) { return /\\.(png|jpe?g)$/i.test(file?.name || ''); }","tryCatchPattern":"try { await uploadImage(file); } catch (BusinessException e) { if (e.getCode() == 8001) { showToast(\"Please upload a png or jpg image\"); } else throw e; }","preventionTips":["Ensure uploaded files carry proper extensions","Use multipart/form-data with the correct 'file' field name","Pre-validate extensions client-side before upload","Handle extensionless files by prompting users to rename"],"tags":["file-upload","validation","filename","http"],"backgroundTag":"invalid-argument-format","analyzedSha":"5e758547a83371a5a4b29dadf4ac03e8dd527635","analyzedAt":"2026-09-12T08:03:51.356Z","contentChangedAt":"2026-09-12T08:03:51.356Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}