3

I have the following requirement: We have a page that requires a user to enter some data into a form that consists of various input fields and dropdowns. Special is, imho, that within this form is a part for fileupload. The user shall there be able to upload some (multiple) files. Then when he/she presses submit at the end of the page, the whole input shall be submitted and written to an object. In this object also the path to the uploaded files must be saved. I want to achieve the file upload with primefaces' (3.4) component.

Now to my challenge: The fileUpload requires its own h:form with enctype "multipart/form-data". The other data is within a "normal" . For me this means that I must place the forms after each other within the page. But the fileUpload component shall be displayed in the middle of the other form.

How can I achieve this? Any ideas?

Best regards, Florian

Florian Huonder
  • 461
  • 1
  • 7
  • 17

1 Answers1

4

Now to my challenge: The fileUpload requires its own h:form with enctype "multipart/form-data". The other data is within a "normal" . For me this means that I must place the forms after each other within the page.

This makes honestly no sense. I'm not sure how you come up to this conclusion. Perhaps you concluded this based on an incorrect observation of a problem. Perhaps you used the wrong bean scope and encountered null values for all so far entered input and previously uploaded files while processing the form submit. You should be using @ViewScoped if you want the very same bean instance to live as long as you're interacting with the same view by several ajax requests.

You can perfectly put "normal" input fields like <h:inputText> in the very same form with multipart/form-data encoding. With this encoding, the HTTP request is just been sent in a bit different format which allows room for binary content. See also params not getting passed to backing bean for h:commandLink under rich:popupPanel and t:dataList for a detailed background explanation.

See also:


Unrelated to the concrete problem, I'm not sure how to interpret

In this object also the path to the uploaded files must be saved

but I'd like to note that you can absoutely not rely on the full client side file paths being sent to the server. This would only occur when the IE browser is been used due to a security bug in that browser. You should rather be interested in the file contents in flavor of InputStream or byte[]. You should be autogenerating a (temp) file with an unique name yourself, if necessary based on the sole filename (and make sure that a second uploaded file with coincidentally the same filename doesn't override it). See also How to get the file path from HTML input form in Firefox 3

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555