MCPcopy Index your code
hub / github.com/pywebio/PyWebIO / file_upload

Function file_upload

pywebio/input.py:581–652  ·  view source on GitHub ↗

r"""File uploading :param accept: Single value or list, indicating acceptable file types. The available formats of file types are: * A valid case-insensitive filename extension, starting with a period (".") character. For example: ``.jpg``, ``.pdf``, or ``.doc``. * A valid MIME

(label: str = '', accept: Union[List, str] = None, name: str = None, placeholder: str = 'Choose file',
                multiple: bool = False, max_size: Union[int, str] = 0, max_total_size: Union[int, str] = 0,
                required: bool = None, help_text: str = None, **other_html_attrs)

Source from the content-addressed store, hash-verified

579
580
581def file_upload(label: str = '', accept: Union[List, str] = None, name: str = None, placeholder: str = 'Choose file',
582 multiple: bool = False, max_size: Union[int, str] = 0, max_total_size: Union[int, str] = 0,
583 required: bool = None, help_text: str = None, **other_html_attrs):
584 r"""File uploading
585
586 :param accept: Single value or list, indicating acceptable file types. The available formats of file types are:
587
588 * A valid case-insensitive filename extension, starting with a period (".") character. For example: ``.jpg``, ``.pdf``, or ``.doc``.
589 * A valid MIME type string, with no extensions.
590 For examples: ``application/pdf``, ``audio/*``, ``video/*``, ``image/*``.
591 For more information, please visit: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types
592
593 :type accept: str or list
594 :param str placeholder: A hint to the user of what to be uploaded. It will appear in the input field when there is no file selected.
595 :param bool multiple: Whether to allow upload multiple files. Default is ``False``.
596 :param int/str max_size: The maximum size of a single file, exceeding the limit will prohibit uploading.
597 The default is 0, which means there is no limit to the size.
598
599 ``max_size`` can be a integer indicating the number of bytes, or a case-insensitive string ending with `K` / `M` / `G`
600 (representing kilobytes, megabytes, and gigabytes, respectively).
601 E.g: ``max_size=500``, ``max_size='40K'``, ``max_size='3M'``
602
603 :param int/str max_total_size: The maximum size of all files. Only available when ``multiple=True``.
604 The default is 0, which means there is no limit to the size. The format is the same as the ``max_size`` parameter
605 :param bool required: Indicates whether the user must specify a file for the input. Default is ``False``.
606 :param - label, name, help_text, other_html_attrs: Those arguments have the same meaning as for `input()`
607 :return: When ``multiple=False``, a dict is returned::
608
609 {
610 'filename': file name,
611 'content':content of the file (in bytes),
612 'mime_type': MIME type of the file,
613 'last_modified': Last modified time (timestamp) of the file
614 }
615
616 If there is no file uploaded, return ``None``.
617
618 When ``multiple=True``, a list is returned. The format of the list item is the same as the return value when ``multiple=False`` above.
619 If the user does not upload a file, an empty list is returned.
620
621 .. note::
622
623 If uploading large files, please pay attention to the file upload size limit setting of the web framework.
624 When using :func:`start_server() <pywebio.platform.tornado.start_server>` or
625 :func:`path_deploy() <pywebio.platform.path_deploy>` to start the PyWebIO application,
626 the maximum file size to be uploaded allowed by the web framework can be set through the ``max_payload_size`` parameter.
627
628 .. exportable-codeblock::
629 :name: file_upload_example
630 :summary: `file_upload()` example
631
632 # Upload a file and save to server # ..doc-only
633 f = input.file_upload("Upload a file") # ..doc-only
634 open('asset/'+f['filename'], 'wb').write(f['content']) # ..doc-only
635
636 imgs = file_upload("Select some pictures:", accept="image/*", multiple=True)
637 for img in imgs:
638 put_image(img['content'])

Callers 4

put_file_uploadFunction · 0.90
basic_inputFunction · 0.85
mainFunction · 0.85
formFunction · 0.85

Calls 3

_parse_argsFunction · 0.85
parse_file_sizeFunction · 0.85
single_inputFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…