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 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. ---
  2. title: Upload
  3. order: 26
  4. layout: page
  5. ---
  6. [[components.upload]]
  7. = [classname]#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 displays a file name entry box, a file selection button, and an upload submit
  14. button. The user can either write the filename in the text area or click the
  15. [guibutton]#Browse# button to select a file. After the file is selected, the
  16. user sends the file by clicking the upload submit button.
  17. Uploading requires a receiver that implements [interfacename]#Upload.Receiver#
  18. to provide an output stream to which the upload is written by the server.
  19. [source, java]
  20. ----
  21. Upload upload = new Upload("Upload it here", receiver);
  22. ----
  23. [[figure.ui.upload]]
  24. .The [classname]#Upload# component
  25. image::img/upload.png[width=60%, scaledwidth=80%]
  26. You can set the text of the upload button with [methodname]#setButtonCaption()#.
  27. Note that it is difficult to change the caption or look of the
  28. [guibutton]#Browse# button. This is a security feature of web browsers. The
  29. language of the [guibutton]#Browse# button is determined by the browser, so if
  30. you wish to have the language of the [classname]#Upload# component consistent,
  31. you will have to use the same language in your application.
  32. [source, java]
  33. ----
  34. upload.setButtonCaption("Upload Now");
  35. ----
  36. You can also hide the upload button with [literal]#++.v-upload .v-button
  37. {display: none}++# in theme, have custom logic for starting the upload, and call
  38. [methodname]#startUpload()# to start it. If the upload component has
  39. [methodname]#setImmediate(true)# enabled, uploading starts immediately after
  40. choosing the file.
  41. [[components.upload.receiving]]
  42. == Receiving Upload Data
  43. The uploaded files are typically stored as files in a file system, in a
  44. database, or as temporary objects in memory. The upload component writes the
  45. received data to an [classname]#java.io.OutputStream# so you have plenty of
  46. freedom in how you can process the upload content.
  47. To use the [classname]#Upload# component, you need to implement the
  48. [classname]#Upload.Receiver# interface. The [methodname]#receiveUpload()# method
  49. of the receiver is called when the user clicks the submit button. The method
  50. must return an [classname]#OutputStream#. To do this, it typically creates a
  51. file or a memory buffer to which the stream is written. The method gets the file
  52. name and MIME type of the file, as reported by the browser.
  53. While uploading, the upload progress can be monitored with an
  54. [interfacename]#Upload.ProgressListener#. The [methodname]#updateProgress()#
  55. method gets the number of read bytes and the content length as parameters. The
  56. content length is reported by the browser, but the reported value is not
  57. reliable, and can also be unknown, in which case the value is -1. It is
  58. therefore recommended to follow the upload progress and check the allowed size
  59. in a progress listener. Upload can be terminated by calling
  60. [methodname]#interruptUpload()# on the upload component. You may want to use a
  61. [classname]#ProgressBar# to visualize the progress, and in indeterminate mode if
  62. the content length is not known.
  63. When an upload is finished, successfully or unsuccessfully, the
  64. [classname]#Upload# component will emit the [classname]#Upload.FinishedEvent#
  65. event, which you can handle with an [classname]#Upload.FinishedListener# added
  66. to the upload component. The event object will include the file name, MIME type,
  67. and final length of the file. More specific [classname]#Upload.FailedEvent# and
  68. [classname]#Upload.SucceededEvent# events will be called in the cases where the
  69. upload failed or succeeded, respectively.
  70. The following example uploads images to [filename]#/tmp/uploads# directory in
  71. (UNIX) filesystem (the directory must exist or the upload fails). The component
  72. displays the uploaded image in an [classname]#Image# component.
  73. [source, java]
  74. ----
  75. // Show uploaded file in this placeholder
  76. final Embedded image = new Embedded("Uploaded Image");
  77. image.setVisible(false);
  78. // Implement both receiver that saves upload in a file and
  79. // listener for successful upload
  80. class ImageUploader implements Receiver, SucceededListener {
  81. public File file;
  82. public OutputStream receiveUpload(String filename,
  83. String mimeType) {
  84. // Create upload stream
  85. FileOutputStream fos = null; // Stream to write to
  86. try {
  87. // Open the file for writing.
  88. file = new File("/tmp/uploads/" + filename);
  89. fos = new FileOutputStream(file);
  90. } catch (final java.io.FileNotFoundException e) {
  91. new Notification("Could not open file<br/>",
  92. e.getMessage(),
  93. Notification.Type.ERROR_MESSAGE)
  94. .show(Page.getCurrent());
  95. return null;
  96. }
  97. return fos; // Return the output stream to write to
  98. }
  99. public void uploadSucceeded(SucceededEvent event) {
  100. // Show the uploaded file in the image viewer
  101. image.setVisible(true);
  102. image.setSource(new FileResource(file));
  103. }
  104. };
  105. ImageUploader receiver = new ImageUploader();
  106. // Create the upload with a caption and set receiver later
  107. Upload upload = new Upload("Upload Image Here", receiver);
  108. upload.setButtonCaption("Start Upload");
  109. upload.addSucceededListener(receiver);
  110. // Put the components in a panel
  111. Panel panel = new Panel("Cool Image Storage");
  112. Layout panelContent = new VerticalLayout();
  113. panelContent.addComponents(upload, image);
  114. panel.setContent(panelContent);
  115. ----
  116. See the http://demo.vaadin.com/book-examples-vaadin7/book#component.upload.basic[on-line example, window="_blank"].
  117. Note that the example does not check the type of the uploaded files in any way,
  118. which will cause an error if the content is anything else but an image. The
  119. program also assumes that the MIME type of the file is resolved correctly based
  120. on the file name extension. After uploading an image, the component will look as
  121. shown in <<figure.ui.upload.example>>.
  122. [[figure.ui.upload.example]]
  123. .Image Upload Example
  124. image::img/upload-example.png[width=60%, scaledwidth=80%]
  125. [[components.upload.css]]
  126. == CSS Style Rules
  127. [source, css]
  128. ----
  129. .v-upload { }
  130. .gwt-FileUpload { }
  131. .v-button { }
  132. .v-button-wrap { }
  133. .v-button-caption { }
  134. ----
  135. The [classname]#Upload# component has an overall [literal]#++v-upload++# style.
  136. The upload button has the same structure and style as a regular
  137. [classname]#Button# component.