Tutorial how to upload files using FTP in max via maxscript and dotnet

You can use my FTPUpload script, but for the scripters here some code. In this tutorial you will create a script that upload your scene to your server.

What you will be creating:

Let’s start by creating a new script. For this tutorial we will uploading our scene. So first we have to check if the current scene is saved. The variable maxFilePath contains a String value that defines the file name for the currently open scene. If this is == “”, let’s save the current scene in de default directory (and delete it afterwards).

checkForSave() checks if the latest saved version is outdated. If this is the case the user will be prompt if he wants to save a newer version. Here’s the code.

if maxFilePath == "" do
(
    saved = saveMaxFile (getdir #scene + "\\" + "FTPUploadTemp.max")
    if not saved do messageBox "You have to save your file first." \
        title:"FTPUpload"
)
checkForSave()

For uploading a file we need a dotnetObjectSystem.Net.WebClient (“Provides common methods for sending data to and receiving data from a resource identified by a URI.“).  (ftp://example.com/example.max).

We will use the UploadFileAsync(Uri, String) method (“Uploads the specified local file to the specified resource, using the POST method. This method does not block the calling thread.”). So let’s type :

if maxFilePath != "" do
(
    sceneName = maxFilePath + maxFileName
    webClient = dotnetObject "System.Net.WebClient"
    webClient.Credentials = dotnetObject "System.Net.NetworkCredential"\
        "user" "pass"
    theUri=dotnetObject "System.Uri" \
        ("ftp://josbalcaen.com/josbalcaen.com/www/public/"+maxFileName)
    webClient.UploadFileAsync theUri sceneName
)

This code should work. The file is uploaded to our directory. But how do we know if the upload is completed? Let’s make a rollout with a progressbar!

Put the code from above in the event on btnUpload pressed do (…insert code here…).


rollout rollFTPUpload "FTP Upload" width:200
(
    edittext edtFtp "FTP" text:"ftp://example.com/" \
        width:(rollFTPUpload.width-5) align:#center
    edittext edtUser "USER" text:"" width:(rollFTPUpload.width-5) \
        align:#center
    edittext edtPass "PASS" text:"" width:(rollFTPUpload.width-5) \
        align:#center
    button btnUpload "Upload" width:(rollFTPUpload.width-5) align:#center
    progressbar pgbProgress width:(rollFTPUpload.width-5) \
        align:#center color:(color 0 255 0) height:5 value:100

    on btnUpload pressed do
    (

    )
)
createDialog rollFTPUpload style:#(#style_toolwindow,#style_sysmenu)

Now we have a rollout with some text fields and a button. If you press the button the file should upload fine. To use the edittext , replace “user”  with edtUser.text and similar for pass and ftp. Now we have to define 2 functions, UploadProgressChanged and  UploadFileCompleted. You can name this like you want, but we will name it the same as the eventHandlers for obviousness.

Also move the code that creates the webClient to the top. So let’s put the following code above :

function UploadProgressChanged = ()
function UploadFileCompleted = ()

webClient = dotnetObject "System.Net.WebClient"

So let’s add the functions. In the UploadProgressChanged function we simply update the progressbar. If the file is uploaded, the UploadFileCompleted function will be called. We also have to tell the webClient object to use these functions with addEventHandler. We do this when the dialog opens.

Add this code betweenprogressbar pgbProgress” and “on btnUpload pressed“.

function UploadProgressChanged sender args =
(
    pgbProgressFile.value=args.ProgressPercentage
)

function UploadFileCompleted sender args =
(
    if args.Error==undefined do
    (
         messageBox ("file has uploaded succesfull! \n " +\
               edtFtp.text + maxFileName)
    )
)
on rollFTPUpload open do
(
    dotnet.addEventHandler webClient "UploadProgressChanged" \
         UploadProgressChanged
    dotnet.addEventHandler webClient "UploadFileCompleted" \
         UploadFileCompleted
)

Done! If you want to upload more than one file, you should do this if the previous file is completed. Give a reply if everythings fine,
Cheers!


3 Comments

chen · August 13, 2017 at 10:52 am

I wrote a simple script, but I don’t know how to write the progress bar. Can you help me change it?

global downloadfilex
try(destroydialog downloadfilex)catch()
rollout downloadfilex “FTPDownloadFile” width:303 height:93
(
progressBar pgbProgressFile “ProgressBar” pos:[4,52] width:290 height:12
button xiazai “DownloadFile” pos:[6,6] width:288 height:19
Timer tmr1 “Timer” pos:[251,28] width:24 height:24 interval:500 active:false
on downloadfilex open do
(

)
on downloadfilex close do
()
on xiazai pressed do
(
webClient = dotnetObject “System.Net.WebClient”
webClient.Credentials = dotnetObject “System.Net.NetworkCredential” “chenjinggang” “123456789”
theUri=dotnetObject “System.Uri” (“ftp://192.168.1.102″+”//”+”cs.zip”)
webClient.DownloadFile theUri “d://cs.zip”

)

)
createdialog downloadfilex pos:[220 ,100]

chen · August 13, 2017 at 10:46 am

Thank you

chen · August 13, 2017 at 10:45 am

Thank you very much. FT upload is really easy to use. Can you write a FTP download script?

Leave a Reply to chen Cancel reply

Your email address will not be published. Required fields are marked *