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 · examples/views/store_files.py · sjs:store-files.server
View on GitHub
# POST /api/files — store uploads, return their ids in the same order
def upload(request: HttpRequest) -> JsonResponse:
# Field "files[]", the name on every platform. Not request.FILES["files[]"]: that is only the last file
files = request.FILES.getlist("files[]")
if not files:
return JsonResponse({"error": 'Upload one or more files in a field named "files[]"'}, status=422)
types = [detect_type(file.read(4096)) for file in files] # by the bytes, never the client's type
for file, type in zip(files, types): # check the whole batch before storing any
if file.size > MAX_BYTES:
return JsonResponse({"error": "Each file must be 5 MB or less"}, status=422)
if type not in TYPES:
return JsonResponse({"error": f"{file.name} is not a PNG, JPEG, WebP, GIF or PDF file"}, status=422)
storage, ids = uploads(request), []
for file, type in zip(files, types):
id = str(uuid.uuid4())
storage.save(id, file) # named by id, never by the client
File.objects.create(id=id, name=file.name, type=type)
ids.append(id)
return JsonResponse(ids, safe=False)
Server · examples/views/creator_load_save.py · sjs:creator-load-save.server
View on GitHub
# PUT /api/forms/<id> — store the definition as-is: it's one JSON document
def put_form(request: HttpRequest, form_id: str) -> HttpResponse:
if not can(request.user, "edit-forms"): # editing is an admin action: 403 otherwise
return JsonResponse({"error": "You don't have editor rights: only editors can change forms"}, status=403)
# The raw body, not request.POST or a serializer: a serializer would drop the keys it doesn't declare
Form.objects.update_or_create(key=form_id, defaults={"json": dumps(read_json(request))}) # {} stays {}, 1.0 stays 1.0
return HttpResponse(status=204) # the next page load renders the new version