You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

components-upload.asciidoc 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. ---
  2. title: Upload
  3. order: 26
  4. layout: page
  5. ---
  6. [[components.upload]]
  7. = Upload
  8. ifdef::web[]
  9. [.sampler]
  10. image:{live-demo-image}[alt="Live Demo", link="http://demo.vaadin.com/sampler/#ui/data-input/other/upload"]
  11. endif::web[]
  12. The [classname]#Upload# component allows a user to upload files to the server.
  13. It has two different modes controlled with [methodname]#setImmediateMode(boolean)#, that affect the user workflow.
  14. [parameter]#Immediate# (default):: In the immediate mode, the upload displays a file name entry box and a button for selecting the file. The uploading is started immediately after the file has been selected.
  15. [parameter]#Non-immediate#:: In the non-immediate mode, the upload displays a file name
  16. entry box, a button for selecting the file and a button for starting the upload.
  17. After the file is selected, the user starts the upload by clicking the submit button.
  18. Uploading requires a receiver that implements [interfacename]#Upload.Receiver#
  19. to provide an output stream to which the upload is written by the server.
  20. [source, java]
  21. ----
  22. Upload upload = new Upload("Upload it here", receiver);
  23. upload.setImmediateMode(false);
  24. ----
  25. [[figure.ui.upload]]
  26. .The [classname]#Upload# component in non-immediate mode
  27. image::img/upload.png[width=60%, scaledwidth=80%]
  28. In the image above, the upload is in non-immediate mode. By default in the immediate mode,
  29. only the [guilabel]#Start Upload# button is visible.
  30. You can set the text of the upload button with [methodname]#setButtonCaption()#.
  31. Note that it is difficult to change the caption or look of the
  32. [guibutton]#Browse# button. This is a security feature of web browsers. The
  33. language of the [guibutton]#Browse# button is determined by the browser, so if
  34. you wish to have the language of the [classname]#Upload# component consistent,
  35. you will have to use the same language in your application.
  36. [source, java]
  37. ----
  38. upload.setButtonCaption("Upload Now");
  39. ----
  40. You can also hide the upload button with [literal]#++.v-upload .v-button
  41. {display: none}++# in theme, have custom logic for starting the upload, and call
  42. [methodname]#startUpload()# to start it.
  43. [[components.upload.receiving]]
  44. == Receiving Upload Data
  45. The uploaded files are typically stored as files in a file system, in a
  46. database, or as temporary objects in memory. The upload component writes the
  47. received data to an [classname]#java.io.OutputStream# so you have plenty of
  48. freedom in how you can process the upload content.
  49. To use the [classname]#Upload# component, you need to implement the
  50. [classname]#Upload.Receiver# interface. The [methodname]#receiveUpload()# method
  51. of the receiver is called when the user clicks the submit button. The method
  52. must return an [classname]#OutputStream#. To do this, it typically creates a
  53. file or a memory buffer to which the stream is written. The method gets the file
  54. name and MIME type of the file, as reported by the browser.
  55. While uploading, the upload progress can be monitored with an
  56. [interfacename]#Upload.ProgressListener#. The [methodname]#updateProgress()#
  57. method gets the number of read bytes and the content length as parameters. The
  58. content length is reported by the browser, but the reported value is not
  59. reliable, and can also be unknown, in which case the value is -1. It is
  60. therefore recommended to follow the upload progress and check the allowed size
  61. in a progress listener. Upload can be terminated by calling
  62. [methodname]#interruptUpload()# on the upload component. You may want to use a
  63. [classname]#ProgressBar# to visualize the progress, and in indeterminate mode if
  64. the content length is not known.
  65. When an upload is finished, successfully or unsuccessfully, the
  66. [classname]#Upload# component will emit the [classname]#Upload.FinishedEvent#
  67. event, which you can handle with an [classname]#Upload.FinishedListener# added
  68. to the upload component. The event object will include the file name, MIME type,
  69. and final length of the file. More specific [classname]#Upload.FailedEvent# and
  70. [classname]#Upload.SucceededEvent# events will be called in the cases where the
  71. upload failed or succeeded, respectively.
  72. The following example uploads images to [filename]#/tmp/uploads# directory in
  73. (UNIX) filesystem (the directory must exist or the upload fails). The component
  74. displays the uploaded image in an [classname]#Image# component.
  75. [source, java]
  76. ----
  77. // Show uploaded file in this placeholder
  78. final Image image = new Image("Uploaded Image");
  79. // Implement both receiver that saves upload in a file and
  80. // listener for successful upload
  81. class ImageUploader implements Receiver, SucceededListener {
  82. public File file;
  83. public OutputStream receiveUpload(String filename,
  84. String mimeType) {
  85. // Create and return a file output stream
  86. ...
  87. }
  88. public void uploadSucceeded(SucceededEvent event) {
  89. // Show the uploaded file in the image viewer
  90. image.setSource(new FileResource(file));
  91. }
  92. };
  93. ImageUploader receiver = new ImageUploader();
  94. // Create the upload with a caption and set receiver later
  95. Upload upload = new Upload("Upload Image Here", receiver);
  96. upload.addSucceededListener(receiver);
  97. ----
  98. [[components.upload.css]]
  99. == CSS Style Rules
  100. [source, css]
  101. ----
  102. .v-upload { }
  103. .gwt-FileUpload { }
  104. .v-button { }
  105. .v-button-wrap { }
  106. .v-button-caption { }
  107. ----
  108. The [classname]#Upload# component has an overall [literal]#++v-upload++# style.
  109. The upload button has the same structure and style as a regular
  110. [classname]#Button# component.