JSON definition · shared/definitions/creator-uploads.json
View on GitHub
{
"title": "Customer support",
"description": "Tell us how we can help",
"elements": [
{
"type": "image",
"name": "help_image",
"imageLink": "/shared/samples/support-help.svg",
"altText": "Support resources",
"imageHeight": "180",
"imageWidth": "100%"
},
{
"type": "dropdown",
"name": "product",
"title": "Product",
"choices": [
"Form Library",
"Survey Creator",
"Dashboard",
"PDF Generator"
]
},
{
"type": "comment",
"name": "issue",
"title": "How can we help?",
"isRequired": true
}
]
}
Client · shared/client/creator-uploads.js · sjs:creator-uploads.client
View on GitHub
// Store each uploaded image separately and keep its URL in the form schema
creator.onUploadFile.add(async (_, options) => {
const label = options.propertyName === "logo" || options.elementType === "header" ? "Logo" : "Image";
uploads++; updateControls();
showError("");
try {
if (!options.files.length || options.files.some(file =>
file.size > 5242880 || !["image/png", "image/jpeg", "image/webp", "image/gif"].includes(file.type)))
throw new Error("Choose a PNG, JPEG, WebP, or GIF image up to 5 MB.");
const body = new FormData();
options.files.forEach(file => body.append("files[]", file));
const response = await fetch("/api/files", { method: "POST", body });
const ids = response.ok ? await response.json() : null;
if (!Array.isArray(ids) || ids.length !== options.files.length ||
ids.some(id => typeof id !== "string" || !id))
throw new Error(`Couldn't upload the ${label.toLowerCase()}. Please try again.`);
options.callback("success", `/api/files/${encodeURIComponent(ids[0])}`);
note(`${label} uploaded.`, "ok");
} catch (error) {
options.callback("error");
showError(error.message);
note(error.message, "error");
} finally { uploads--; updateControls(); }
});
Server · src/main/java/io/surveyjs/examples/StoreFilesController.java · sjs:store-files.server
View on GitHub
// POST /api/files — store uploads, return their ids in the same order
@PostMapping(path = "/api/files", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public List<String> storeFiles(MultipartHttpServletRequest request) throws IOException {
List<MultipartFile> files = request.getFiles("files[]"); // the name on every platform: PHP keeps only the last of several "files"
if (files.isEmpty()) {
throw new ResponseStatusException(HttpStatus.UNPROCESSABLE_CONTENT, "Upload one or more files in a field named \"files[]\"");
}
for (MultipartFile file : files) { // check the whole batch before storing any
if (file.getSize() > MAX_BYTES) {
throw new ResponseStatusException(HttpStatus.UNPROCESSABLE_CONTENT, "Each file must be 5 MB or less");
}
if (FileType.detect(file) == null) { // PNG, JPEG, WebP, GIF or PDF by the bytes, never the client's Content-Type
throw new ResponseStatusException(HttpStatus.UNPROCESSABLE_CONTENT, file.getOriginalFilename() + " is not a PNG, JPEG, WebP, GIF or PDF file");
}
}
List<String> ids = new ArrayList<>();
for (MultipartFile file : files) {
String id = UUID.randomUUID().toString(); // named by id, never by the client
fileStore.write(id, file);
jdbc.sql("INSERT INTO files (id, name, type) VALUES (?, ?, ?)").params(id, file.getOriginalFilename(), FileType.detect(file)).update();
ids.add(id);
}
return ids;
}
Server · src/main/java/io/surveyjs/examples/CreatorLoadSaveController.java · sjs:creator-load-save.server
View on GitHub
// PUT /api/forms/{id} — store the definition as-is: it's one JSON document
@PutMapping("/api/forms/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
@PreAuthorize("hasAuthority('edit-forms')") // editing is an admin action: 403 otherwise
public void saveForm(@PathVariable String id, @RequestBody JsonNode definition) {
// A JsonNode, not a DTO: binding to a class would drop the keys it doesn't declare
storedJson.put("forms", id, definition); // stored as it arrived: {} stays {}, 1.0 stays 1.0
// 204: the next page load renders the new version
}