Message Boards Message Boards

Can I use file upload in a FormPage? If so, how does it work?

I'm trying to CloudDeploy a simple FormPage where the user can upload a small plaintext file and the code then does stuff with the file contents. But I can't figure out how to do the file upload. From the Documentation, it looks like the "UploadedFile" interpreter type is what I need, so here's a dummy example:

CloudDeploy[FormPage[{"file"->"UploadedFile"},"Not doing anything with that file."&],Permissions->"Public"]

Using this, I get a nice little webpage with an input field that offers drag&drop or browsing to add a file. If I drag&drop, the file name flashes up for a second or so, and then the field turns red and shows the "This field is required" warning, no sign of the file. If I click the field to browse for a file, I do get a browser window, but as soon as I select a file, the same warning appears and again, no file.

I'm pretty sure I'm making a really obvious mistake here, so any pointers would be appreciated.

(I'm using Mathematica 10.4, Windows 10, and Firefox.)

POSTED BY: Bianca Eifert
2 Replies

Christopher, that works great, thank you. In fact, I don't even have to restrict the file type, the following also works for me:

CloudDeploy[
 FormPage[{"file" -> <|"Interpreter" -> "UploadedFile", 
     "AutoSubmitting" -> False|>}, 
  "Not doing anything with that file." &], Permissions -> "Public"]

For future reference, how could I have found the "AutoSubmitting" option myself? I did find the syntax of using an Association on the Documentation page for FormFunction, but are the keys supported for this syntax listed anywhere? In particular, do you perhaps know if there's an option that lets the input fields stay filled when the form is submitted so that a user can go back and forth to make edits to one field while keeping the others? This seems to work just fine for all other fields, but the file upload field is awfully forgetful...

POSTED BY: Bianca Eifert

Specify that the FormPage UploadedFile input field should not automatically submit the form:

CloudDeploy[
   FormPage[
      {"x" -> <|"Interpreter" -> Restricted["UploadedFile", "Text"], "AutoSubmitting" -> False|> },
      Import[#x, "Text"] & ], 
   FileNameJoin[{$CloudRootDirectory, "FormPageTextRestrictedAutoSubmittingFalse"}] ]
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract