Message Boards Message Boards

0
|
20843 Views
|
7 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Upload xl sheet via webform in cloud

Posted 10 years ago

Hello everyone,

I just joined the Wolfram community. I'am using Mathematica for data processing now for 1,5 years. The new association and dataset functions are super. I use less and less Excel. Here is my question. Spend a day on it but did not find the right direction. Can anyone give me a tip or trick? What I want is to create a webform that enables the upload of a Excel sheet. The sheet is then stored in a cloud directory.

This piece of code worked from the desktop when providing a path-filename.

f[upl_] := Interpreter["UploadedFile"][upl["pdd"]]
form = FormFunction[{"pdd" -> "FileName"}, f[#] &]
form[]

My next step was something like this:

CloudDeploy[FormFunction[{"fname"->"UploadedFile"},store-it-function]]

Provides me with a webform and a nice browser button to pickup the file. But I have no idea how to get the .xlsx file stored in the cloud. Can you help?

Kind regards, Pieter

7 Replies

Hoi Pieter, Not a perfect solution, but it is possible to upload an Excel file as binary and then you can import it after first converting the binary to xlsx, like so:

CloudDeploy@
     FormFunction[{"data" -> "Binary"}, (CloudPut[#data, "excelFile"]; 
        "Uploaded XLSX") &]

Composition[ImportString[#, "XLSX"] &,
                           ExportString[#, "Binary"] &,
                           CloudGet
  ]@"excelFile"

Rolf

POSTED BY: Rolf Mertig

Hello Emerson,
Your first function using CSV files does work and uploads as a list object in the cloud. For some strange reason I can't get the XLSX upload FormFunction to work. After submitting, the file name is removed and an error "upload a file of type XLSX" message appears in red. What could be the cause of this?
My main goal is to upload a xlsx file as is. No translation to list object. Is that easier? kind regards, Pieter

Emerson, thanks for checking this. Will do some tests myself and provide feedback.

Import of XLSX now works.

form9=FormFunction[{"data"->"XLSX"},(CloudPut[#data,"myfile9"];"Completed")&]
CloudDeploy[form9]
POSTED BY: Emerson Willard

The issue is not solved but I was able to generate error messages that may help a more knowledgeable person reading this (goal is upload XLS sheet via Wolfram Cloud Web Form Submit)

FormFunction[
  "fname" -> "String", 
  Module[{},
    st1 = "E:\\testfolder\\" <> #fname;
    st2 = #fname;
    SetCloudDirectory[$CloudRootDirectory];
    SetCloudDirectory["results"];
    zz = CloudObject[st2];
    {EvaluationData[CopyFile[st1, zz]]}
    ] &
  ][]

This (above) runs from the Mathematica desktop application (and works fine) and generates the following evaluation data:

{<|"Result" -> CloudObject[
   "https://www.wolframcloud.com/objects/user-9b714558-a731-4b6e-9313-\
00431f13aa06/results/xlsbook4.xls"], "Success" -> True, 
  "FailureType" -> None, "Messages" -> {}, "MessagesText" -> {}, 
  "MessagesExpressions" -> {}, "Timing" -> 1.076, 
  "AbsoluteTiming" -> 2.48, "InputString" -> "CopyFile[st1, zz]"|>}

The next example is extended with CloudDeploy and is NOT a success. See error message generated by webform:

--code --
CloudDeploy[FormFunction[
  "fname" -> "String", 
  Module[{},
    st1 = "E:\\testfolder\\" <> #fname;
    st2 = #fname;
    SetCloudDirectory[$CloudRootDirectory];
    SetCloudDirectory["results"];
    zz = CloudObject[st2];
    {EvaluationData[CopyFile[st1, zz]]}
    ] &
  ]]

(error message )

{<|"Result" -> $Failed, "Success" -> False, 
      "FailureType" -> "MessageFailure", 
      "Messages" -> {FileFormat::nffil, StringMatchQ::strse, 
        BinaryReadList::nffil, CloudObject::srverr}, 
      "MessagesText" -> {"FileFormat::nffil : File not found during \
    FileFormat[E:\\testfolder\\xlsbook4.xls].", 
        "StringMatchQ::strse : String or list of strings expected at \
    position 1 in StringMatchQ[$Failed, __~~/~~__].", 
        "BinaryReadList::nffil : File not found during \
    BinaryReadList[E:\\testfolder\\xlsbook4.xls].", 
        "CloudObject::srverr : An unknown server error occurred."}, 
      "MessagesExpressions" -> {Hold[
         Message[FileFormat::nffil, 
          HoldForm[FileFormat["E:\\testfolder\\xlsbook4.xls"]]]], 
        Hold[Message[StringMatchQ::strse, HoldForm[1], 
          HoldForm[StringMatchQ[$Failed, __ ~~ "/" ~~ __]]]], 
    Hold[Message[BinaryReadList::nffil, 
      HoldForm[BinaryReadList["E:\\testfolder\\xlsbook4.xls"]]]], 
    Hold[Message[CloudObject::srverr]]}, "Timing" -> 0.01, 
  "AbsoluteTiming" -> 0.01, "InputString" -> "CopyFile[st1, zz]"|>}

The code below is including the File[st1] command and seems to work a little better but still Success is false: ( looks like there is no access to the local disk anymore... but how to provide that access)

(* code *)

CloudDeploy[FormFunction[
  "fname" -> "String", 
  Module[{},
    st1 = "E:\\testfolder\\" <> #fname;
    st2 = #fname;
    SetCloudDirectory[$CloudRootDirectory];
    SetCloudDirectory["results"];
    zz = CloudObject[st2];
    {EvaluationData[CopyFile[File[st1], zz]]}
    ] &
  ]]

(* error data *)

{<|"Result" -> 
   CloudObject[
    "https://www.wolframcloud.com/objects/user-9b714558-a731-4b6e-\
9313-00431f13aa06/results/xlsbook5.xls"], "Success" -> False, 
  "FailureType" -> "MessageFailure", 
  "Messages" -> {StringMatchQ::strse, 
    "`1` is not a string, InputStream[ ], or OutputStream[ ]."}, 
  "MessagesText" -> {"StringMatchQ::strse : String or list of strings \
expected at position 1 in StringMatchQ[FileFormat[File[E:\\testfolder\
\\xlsbook5.xls]], __~~/~~__].", 
    "General::stream : File[E:\\testfolder\\xlsbook5.xls] is not a \
string, InputStream[ ], or OutputStream[ ]."}, 
  "MessagesExpressions" -> {Hold[
     Message[StringMatchQ::strse, HoldForm[1], 
      HoldForm[
       StringMatchQ[
        FileFormat[File["E:\\testfolder\\xlsbook5.xls"]], __ ~~ 
         "/" ~~ __]]]], 
    Hold[Message[General::stream, 
      HoldForm[File["E:\\testfolder\\xlsbook5.xls"]]]]}, 
  "Timing" -> 1.0230000000000001, "AbsoluteTiming" -> 1.27, 
  "InputString" -> "CopyFile[File[st1], zz]"|>}

hope this info helps with ideas on how to resolve this issue. Kind Regards, pvanbijnen.

Thanks Emerson for your ideas. I am still looking for a way to upload XLS files via a webform. Does anyone have Ideas on how to upload a file via a webform.

Spend a day trying things like this:

CloudDeploy @
 FormFunction[
  "fname" -> "String", 
  Module[{st1, st2},
    st1 = "E:\\math9\\Personal Lessons\\" <> #fname <> ".xls";
    st2 = #fname <> ".xls";
    {st1, st2, CopyFile[st1, CloudObject[st2]]}
    ] &
  ]

CopyFile seems to work only from the desktop and not on a webform.

I also tried

CloudDeploy @
 FormFunction[
  "fname" -> Interpreter[Restricted["UploadedFile", "XLS"]],
  {#fname}
  ]

but it looks this does not work because it always produces the following error: error after submitting the file with correct extension

The function isn't even evaluated and I assure you the file submitted had the right type.

can anybody help on how to do this or state that it is not yet possible in the WolframCloud?

kind regards, Pieter van Bijnen

The following works for CSV files.

enter image description here

enter image description here

However, FormFuction failed when "XLS" or "XLSX" were used.

POSTED BY: Emerson Willard
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