# Upload a file using a WolframCloud WebForm

I tried to create a Webform in WolframCloud that asks/browses for a file and then uploads it to a specific folder. The following are the directions I investigated but it does not work:

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


I also tried directions like this:

CloudDeploy @
FormFunction[
{#fname}
]


but it looks this does not work because it always produces the following error:

The function isn't even evaluated. Can anybody help on how to do this or state that it is not yet possible in the WolframCloud?

Kind regards

• xlsfile = "F:\\cloud\\test.xlsx"; CloudDeploy[ FormFunction[ "fname" -> Interpreter[Restricted["UploadedFile", "XLSX"]][ File[xlsfile]], # &], Permissions -> "Public"] at least evaluates and uploads the local xlsfile, but I don't quite know how to incorporate an html input field for file uploading. Maybe it is possible with some JavaScript embedding or some such, but I am short of time now. – Rolf Mertig Oct 15 '14 at 23:01
• Rolf, thank for your initial investigation. Indeed from the desktop copying file to the cloud is easy. The difficulty is to first launch a webform and then allow the webform to pick a file (via the use of UploadedFile) and then making sure the form has access to the local disk to pick up the file and copy it to the cloud. My first example (with CopyFile) shows that the file is created in the cloud but has size zero. I think access to the local drive is the issue but don't know for sure. Any help is very welcome.. – pvanbijnen Oct 16 '14 at 13:23
• One more note: I tested the code of Rolf by entering it in the a wolfram cloud notebook. The code does not not work anymore (so its only valid for execution on the desktop is seems) – pvanbijnen Oct 16 '14 at 13:37

This proves the Wolfram Cloud upload webform is capable of picking a file from your disk and upload it to the cloud. Still no idea how it should works for XLS files but if you want to upload a picture file and store it in the same or another format this is how to do it:

CloudDeploy[
FormFunction[{"x" -> "Image"}, (*works fine via internet form*)


The Image is interpreted as a raw content of the form Image[RawArray["byte", "series of numbers"]]. So there is no binary stream of data which i think the upload of a xls file requires... any ideas???

If $CloudBase is set to the default, i.e., you use Wolfram Mathematica 10.3 on the cloud server, then this just seems to work:  CloudDeploy[ FormPage[{{"ex", "Upload XLS file:"} -> "UploadedFile"}, Import[#ex, "XLS"] &]]  or, for xlsx files:  CloudDeploy[ FormPage[{{"ex", "Upload XLSX file:"} -> "UploadedFile"}, Import[#ex, "XLSX"] &]]  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,
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[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:

(* 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.

• Why don't you contact Wolfram support? They should really be able to figure this out. If you don't get an answer by Tuesday let me know: I will be at the WRI conference and can ask around. – Rolf Mertig Oct 17 '14 at 12:10
• Thanks Rolf, I just contacted support by email. Will keep you posted on progress. Thanks for the offer to ask arround at the WRI conference. – pvanbijnen Oct 17 '14 at 14:29