What to pay attention to when uploading files encoded through the interface, volume and verification
When uploading content encoded with base64 through the interface, it is necessary to accept a premise: the transmission is text, not raw bytes. The volume will therefore increase, with an estimate of about one-third larger after encoding, rather than applying quotas directly based on file size. Since version v3.6.5, the ability to upload attachments in base64 has been supported internally, mainly for batch import scenarios without form interaction. The verification is done in three stages: local verification before submission, alignment of the upper limit of the request body, and verification of the returned results.
Application scenarios for two upload methods
multipart form upload is suitable for manual operation: the browser reads the file directly, the request body is transmitted in binary chunks, and the volume is close to the file itself.
base64 upload is suitable for programmatic calls: the caller has already placed the content in memory or configuration, such as exporting data from another system and immediately entering it, script batch supplementing attachments, or putting the content into a JSON request body and sending it out. The cost is increased volume and memory usage, in exchange for a single request structure, which does not need to handle the multipart chunk format.
The criteria are very simple: if the data source is 'files on disk', use a form; if it's 'strings in the program', use encoding upload. When both are mixed, the volume of the same batch of attachments will be inconsistent, making it easy to misjudge later on.
Why does the volume of encoding after become larger, what should be estimated according to
base64 encodes every three bytes of original data into four printable characters, so the length of the encoded text is about three-fourths the size of the original, increasing by one-third. This is a fixed ratio determined by the algorithm, which is unrelated to the file type and compression status.
In practice, estimate in three steps.
Step 1, convert the original size to 'encoded size', reserve one-third by magnifying, and then round up to a convenient level for verification. Step 2, calculate the cost again when putting the encoded string into JSON: escaping, field names, and the outer structure will increase bytes, and this part accumulates significantly when done in bulk. Step 3, verify the upper limit per request rather than the total task volume, because the restriction usually applies to the individual request body.
It is a common mistake to 'batch in the file directly when it is not very large.' What really determines success or failure is the volume of a single request. Taking ten attachments at once is completely different from taking one at a time in ten requests.
Where should the limit of the request body align at several layers?
When a request travels from the caller to the application, it generally passes through several layers, each of which may have its own volume limit. The common four layers are: the caller's own reading and serialization limit, the limit of the request body by the reverse proxy or Web server, the limit of the running environment on the volume or memory of a single request, and the verification of attachment fields and file types at the application layer.
The alignment order is recommended from bottom to top: first confirm what type and size the application accepts, then confirm whether the running environment can accommodate this volume, then check whether the proxy layer will directly reject larger requests, and finally adjust the batch strategy of the caller.
It is faster to judge at which level it is rejected by looking at the return form than by looking at the error text. When the proxy layer directly rejects, it often leaves no trace in the application logs; when the application layer rejects, it is usually possible to read clear field validation information. When troubleshooting, first confirm whether the request has reached the application, which can save half of the invalid work.
One thing that is easily overlooked: Encoding upload will put the entire content into memory, and the peak memory usage during batch concurrent operations is accumulated according to 'concurrent number multiplied by single volume'. A volume-appropriate single request may not be suitable under concurrent conditions.
分工 of import interface and public capability list
The batch import document content import interface /api/import/archive supports ZIP compressed packages and Excel spreadsheet batch forms, and supports querying whether the content exists by title. The latter is very critical in retry scenarios: when the network is interrupted or the task is rerun, confirm the title first before deciding whether to resubmit, which is safer than unconditional resending.
The public ability list is responsible for another matter. The high-frequency capabilities described in the interface document include document lists and details, categories and tags, image upload, comments and reviews, friendship links, system settings and contact information, these are readable or writable public operation surfaces. Its division of labor with the import interface is: the import interface is responsible for 'putting contents in batch', and the ability list is responsible for 'using structures and single-point maintenance'.
Uploading images and uploading attachments are not the same thing. The former serves the display of the main text, while the latter serves the files attached to the document; mixing them up may cause attachments to appear in the image list, or prevent the illustration from being accessed by the image-related capabilities.
Common failure and judgment sequence
Arranged in descending order of frequency
The request is directly rejected by the middle layer, characterized by the application has no logs. The solution is to reduce the batch volume or change to form upload.
The encoding itself is incomplete, characterized by the content is damaged after decoding, common in line breaks and spaces being changed during transmission or copying. The solution is to do a decoding self-check immediately after generation.
The type and extension name are inconsistent, characterized by the application side rejecting or the front end cannot preview. The solution is to explicitly declare the type when submitting, rather than let the server guess.
Duplicate import, characterized by multiple occurrences of the same title. The method is to make an existence judgment by querying by title first.
Memory peak is too high, characterized by tasks becoming slower or failing after running in batches for a period of time. The method is to serialize or limit concurrency instead of continuing to increase the limit.
Common questions
What is the suitable file size for encoding upload?There is no fixed threshold, judge according to whether the volume after encoding falls within the restrictions of each layer. Larger files are more suitable as external storage references or through the form upload channel.
Can a request carry multiple attachments at once?Depends on the interface definition of the request body. In most cases, submitting a single attachment is easier to locate the cause of failure and is also more conducive to rerun.
How to check if the attachment cannot be read on the front end after the upload is successful?First confirm whether the attachment identifier is written in the returned result, and then confirm which document state it is attached to; under the draft state of the document, the visibility of the attachment is different from that of the formal document.
Should you calculate the checksum yourself?It is recommended to keep the original file's checksum information for confirmation that it is consistent with the source file after decoding. This step is more convenient than finding damage after batch migration.
Can the API path be written directly in the text?The interface path within the station belongs to the internal implementation and can appear in the technical documentation; the external address should not be written into the content text, and the two should not be mixed.