Upload images or other file resources to the system, supporting specified resource categories, which can be used to replace existing resources. The system will automatically handle image sizes and generate thumbnails.
Points to note
- This interface uses the POST method, Content-Type is form-data
- To upload a file, the user needs to log in and have the corresponding upload permission
- Supports uploading image files, the system will automatically obtain the image size information
- Can replace existing resources by specifying the id parameter
- The system will automatically generate thumbnails and calculate the MD5 value of the file
Request address
{域名地址}/api/attachment/upload
Description:{域名地址}Replace it with your domain name, such ashttps://en.anqicms.com/api/attachment/upload
Request Syntax
POST {域名地址}/api/attachment/upload
Content-Type: multipart/form-data
Request Headers
This interface involves only public request headers. For more information, please refer toCommon Request Headers (Common Request Headers).
Request parameters
| Field name | Type | Required | Description |
|---|---|---|---|
| category_id | int | No | Image resource category ID |
| id | int | No | Image resource ID, required if replacing an image |
| file | File | Is | To upload the file resource |
Response header
This interface only involves common response headers. For more information, please refer toCommon Request Headers (Common Request Headers).
Return parameters
| Field name | Type | Description |
|---|---|---|
| code | int | Error code |
| msg | string | Error description |
| data | object | result content |
data parameter
| Field name | Type | Description |
|---|---|---|
| id | int | Resource ID |
| file_name | string | Filename |
| file_location | string | File storage path |
| file_size | int | File size |
| file_md5 | string | file MD5 |
| width | int | image width |
| height | int | image height |
| category_id | int | Resource category ID |
| is_image | int | Is it an image, 1 is an image, 0 is not |
| Logo | string | Original |
| thumbnail | string | Thumbnail |
| created_time | int | Timestamp of creation |
| updated_time | int | update timestamp |
| status | int | Display Status |
Usage Example
Request example
POST /api/attachment/upload HTTP/1.1
Host: your-domain.com
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="category_id"
1
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="example.jpg"
Content-Type: image/jpeg
(file content)
------WebKitFormBoundary7MA4YWxkTrZu0gW--
Response Example
{
"code": 0,
"data": {
"id": 213,
"created_time": 1662734332,
"updated_time": 1662734332,
"file_name": "AnqiCMS",
"file_location": "uploads/202209/09/5632dd13c746a365.webp",
"file_size": 9348,
"file_md5": "4fae2c295632dd13c746a3653227f93a",
"width": 160,
"height": 160,
"category_id": 0,
"is_image": 1,
"status": 1,
"logo": "https://en.anqicms.com/uploads/202209/09/5632dd13c746a365.webp",
"thumb": "https://en.anqicms.com/uploads/202209/09/thumb_5632dd13c746a365.webp"
},
"msg": ""
}
error code
| Error code | Description |
|---|---|
| 0 | OK |
| -1 | Error, the reason is indicated in msg |
| 1001 | Not logged in |
| 1002 | Not authorized |
| 200 | API request OK |