JMeter文件上传故障求助:录制场景下PDF/Word无法上传
Hey there, let's work through your JMeter file upload problems—this kind of mismatch between recorded scripts and manual behavior is super common, and we can track it down step by step.
Key Checks & Fixes
1. Verify Content-Type Mismatches for Different File Types
When you upload text files, JMeter might record a generic text/plain Content-Type, but PDFs and Word docs require their own specific types:
- PDF:
application/pdf - Word (.docx):
application/vnd.openxmlformats-officedocument.wordprocessingml.document - Word (.doc):
application/msword
Go to your recorded HTTP request's Files Upload tab. For each file entry, make sure the MIME Type matches the actual file type. If JMeter auto-filled a wrong type (like text/plain for all files), manually update it to the correct value.
2. Compare Recorded Requests with Browser DevTools
Manual uploads work, so let's use your browser's DevTools to capture the "gold standard" request:
- Open Chrome/Firefox DevTools (F12) → go to the Network tab
- Manually upload a PDF/Word doc, then find the POST request responsible for the upload
- Check:
- Request Headers: Ensure JMeter includes all headers from the browser (especially
Content-Disposition,X-CSRF-Tokenif used) - Form Data: Look for hidden parameters (like file hashes, session tokens) that JMeter might have missed during recording. Add these to your JMeter request as user-defined variables or via a preprocessor.
- Filename Encoding: If your filenames have special characters (e.g., Chinese), make sure JMeter uses the same encoding as your browser (usually UTF-8). Set
Content encodingin the HTTP request to match.
- Request Headers: Ensure JMeter includes all headers from the browser (especially
3. Fix Single File vs Folder Upload Request Differences
Since folders upload successfully, compare that request with a failed single file upload:
- Check if the form uses different parameter names (e.g.,
folder_pathvsfile_data) - Verify if the request body structure changes (folders might send a directory path instead of raw file content)
- Ensure the
method(POST/PUT) and endpoint URL are identical for both scenarios—sometimes systems route folder uploads to a different API.
4. Manually Construct the File Upload Request
If recording isn't capturing the right details, build the request from scratch:
- Add a new HTTP Request element
- In the Files Upload tab:
- Click Add to select your PDF/Word doc
- Set the correct MIME Type
- Fill in the
Parameter Name(match what you saw in DevTools, e.g.,upload_file)
- Add any required form parameters (like
csrf_token) from the manual request - Use the View Results Tree listener to compare the JMeter request payload with the browser's—look for missing or incorrect fields.
5. Adjust JMeter Recorder Settings
Sometimes the recorder skips critical data:
- Enable Capture HTTP Headers in the HTTP(S) Test Script Recorder to ensure all headers are recorded
- Check if any filters are blocking the upload request (remove unnecessary filters in the recorder's "URL Patterns to Include")
- For large files, increase the recorder's buffer size (edit
jmeter.properties→ setproxy.max_chunk_sizeto a higher value like10485760)
Final Tip
Always cross-reference every part of your JMeter request against the browser's request—even small differences (like a missing header or wrong MIME type) can break file uploads for non-text files.
内容的提问来源于stack exchange,提问作者user9172019

