4

I have a test file(test1.txt) in my desktop.This is its content.

FilePrint[FileNameJoin[{$HomeDirectory, "Desktop", "test1.txt"}]]
(*
31.16   220.49774362741593
31.18   223.49849688143496
31.2    226.49924900492292
31.22   223.5
31.24   228.5
31.26   231.5
31.28   227.5
31.3    222.5
31.32   222.5
35.72   209.72984917230133
35.74   220.68588083509488
*)

I try to use Import and Export in CloudDeploy.

CloudDeploy[
 FormPage["fileAddress" -> 
   "String", (data = Import[#fileAddress, "Table"]; Sin[data]; 
    Export[FileNameJoin[{$HomeDirectory, "Desktop", "test.txt"}], 
     data]) &], Permissions -> "Public"]

Of course this action will get a error.But I have to say this two function can help a lot in real life.So any method or workaround can do this?


Aim:

I want to create some CloudObject which have some specific function to help my friends who have no mma.I hope she upload test1.txt,then get test.txtin her local disk.Just by her browser.

yode
  • 26,686
  • 4
  • 62
  • 167
  • Is something like this what you're looking for? It gives you an uploader which you can select from a file browser or click&drag into: CloudDeploy[FormFunction[{"data" -> "CSV"}, Print[#data] &]] – ktm Sep 26 '16 at 17:44
  • note the 2nd argument isn't going to work, I just wanted to demonstrate the FormFunction upload page you'll see initially. – ktm Sep 26 '16 at 17:45
  • @user6014 Wow,I have to say this is a wonderful comment.I think.it can replace that Import in my case.BTW,have you summarize other format also can realize such similar function except "CSV".Would you put your comment as a answer?I think it give very useful information to my question.And any workaround for Export? – yode Sep 26 '16 at 18:51
  • I submitted an answer which hopefully helps for half the question, in regards to Export I think it's possible but I don't have time to look into it at the moment. – ktm Sep 26 '16 at 19:01
  • @Kuba The problem of uploading files is seem to be solved by user6014.The remaining problem is how to put a result file into my local disk. – yode Sep 29 '16 at 10:41
  • @Kuba I have made a updation just now. – yode Sep 29 '16 at 10:58

3 Answers3

6

I do not think you can automatically save to someone's file system but you can create a download link, it is up to the user to click and get it:

CloudDeploy[
 FormFunction[
  {"first" -> "Number", "second" -> "Number"}, 
  Module[{f = CreateFile[]},
    StringTemplate[
      "<a href=\"``\" download>Click here to save test.txt</a>"
    ][
       URLShorten @@ CloudExport[#second + #first, "Text", "test.txt", 
         Permissions -> "Public"]
    ]
  ] &
 ],
 Permissions -> "Public"
]

This is an example with two numbers input but you can easily use user6014's suggestion for files uploading

{"data" -> "CSV"}

and process them however you want.

Kuba
  • 136,707
  • 13
  • 279
  • 740
  • I will learn some knowledge of cloud then comment this answer.Thanks. :) – yode Sep 29 '16 at 19:08
  • @yode Sure, let me know how does it work for you or if you need something more. – Kuba Sep 29 '16 at 19:12
  • Not need something more.I just don't very like that link.The network is little weak in China.When I click the link,the browse sometime will give a hint that I cannot access it.So I look forward a file directly still.Well,I think the URLDownload can help.I will give a try for this tomorrow. – yode Sep 29 '16 at 19:23
  • CloudDeploy[FormPage["file"->"Table",(URLDownload[Sequence@@CloudExport[Sin[#file],"Table",Permissions->"Public"],FileNameJoin[{$HomeDirectory,"Desktop","test.txt"}]])&],"testFile",Permissions->"Public"] is what I think.It seem browser cannot do such thing. – yode Oct 02 '16 at 18:49
  • Little blemish,when I change it into CloudExport[Sin[#file], "Table", "test.txt", Permissions -> "Public"].I cannot get a real table file like Export.Actually the file is a table format. – yode Oct 02 '16 at 19:09
  • @yode I will respond in 24h, i am travelling now. – Kuba Oct 02 '16 at 19:12
  • Ok.Great day! :) – yode Oct 02 '16 at 19:14
  • @yode Your code works, but not how you think it should. URLDownload is executed by the Cloud so it tries to download your CloudExported result to Cloud's Desktop directory. FormFunctions/Pages functions are executed on CLoud side and cloud can't have access to users desktop. – Kuba Oct 04 '16 at 09:29
  • Do you mean we have no method access to local desktop?And how about the "Table" format? – yode Oct 04 '16 at 17:05
  • I have tried some methods these days,but I think maybe the browser never cannot write the local disk. – yode Oct 07 '16 at 08:27
  • @yode sure it can;t do that automatically, would you like it to be able do put malicious scripts just because you've opened a page? – Kuba Oct 07 '16 at 08:33
5

FormFunction allows you to upload files directly into the browser, which should bypass your need for Import. If your typespec for your FormFunction is something which Mathematica knows is a file type then it will prompt you to upload a file in the deployed page:

CloudDeploy[FormFunction[{"data" -> "CSV"}, ...

Cloud data import

I believe any of the form specifications for Interpreter are valid typespecs in FormFunction (details and options):

http://reference.wolfram.com/language/ref/Interpreter.html

ktm
  • 4,242
  • 20
  • 28
1

I have to declare that this answer is just combination with user6014's and Kuba's.I compose this for reading.I find the "Table" format maybe have some bug,which don't same to Export[*,"Table"].So I make a workaround by XLS for this target.

CloudDeploy[
 FormPage["file" -> "Table", 
  StringTemplate[
     "Click <a href=\"``\" download>here</a> to save as test.xls"][
    Sequence @@ 
     CloudExport[Sin[#file], "XLS", "test.xls", 
      Permissions -> "Public"]] &], Permissions -> "Public"]

You can use my test1.txt to test this cloud object.If anyone can give a method just using browser to save a file in local disk,I'd glad to kow still.

yode
  • 26,686
  • 4
  • 62
  • 167