Magic User Group

Magic Wiki-Wakka : UploadFileThroughWeb

HomePage :: CATEGORIES | Index :: Changes :: Comments :: Search :: Login/Register

Internet based file upload with Magic


Although you find this information in the „advanced“ section of “Magic & Internet programming“, Internet file uploads with Magic are surprisingly easy to achieve.

Back in 2003 or so I uploaded a sample to the file-section of magicu-l which showed how to achieve this with Magic version 8. Magic version 8 did not support this and the sample (which you can still find at http://tech.groups.yahoo.com/group/magicu-l/files/mghttpup.EXE was based on a little utility from Microsoft Corp. – the “MS web posting acceptor”. This little utility implemented the required server side handling of internet file uploads. Once the file was on the server, it was Magic which took over. Unfortunately Microsoft discontinued this product meanwhile. They expect you to either use Acive server pages (.asp) or Front Page server extensions. This little gem ... has gone.

Since Magic/eDeveloper Version 9 however, Magic has built-in support for internet file-uploads.
Its zhere. Its just not well-known.

With a Magic 9.4 application on server-side you can implement a "RFC 1867 based internet file upload" quite easily.

See the internet draft RFC 1867 - Form-based File Upload in HTML for the "theory" behind these things. Although this protocol its still labeled as an "experimental protocol", its quite standard now. You can hardly find any browser which does not support it.

Client side is easy. Everything you need to do is putting something like this on your html:
	 <FORM ACTION="http://server.dom/cgi/handle"
	       ENCTYPE="multipart/form-data"
	       METHOD=POST>
	 What is your name? <INPUT TYPE=TEXT NAME=submitter>
	 What files are you sending? <INPUT TYPE=FILE NAME=pics>
	 </FORM>


This will effectively give you a html form with a file select button.
If you submit this form, the file will be uploaded.

Server side with Magic 9.4 is even easier:
All you have to do in your server-side Magic program is … write a batch program which has a BLOB as first parameter.
The BLOB will then receive the file as it was sent by the client. THAT'S IT. You can save the file with Blb2File() on your disk/network. The sample creates a directory for each user and puts it there.

I put a full, working, sample to http://tech.groups.yahoo.com/group/magicu-l/files/fileupload_sample.v2.zip. It shows what to do on client-side (html) and what to do on server side. Its really easy and just looks a bit complicated because I also put "session handling" and multi-lingual support in this sample. Internet file-upload alone would reduce to:
- one html-merge template (or static page)
- one Magic program on server side

There's one open issue in Magic which is regarding the filename of the file being sent by the user. Basically that's part of the protocol but its not possible to get the filename.
In the sample you'll see that I solved (worked around) this issue with JavaScript. Works well. The name of the file the user selected then is the second parameter of the receiving Magic program. You get the full path (as it resides on users network). In the sample I remove the path. I am only interested in the filename. Check the sample for more details.

To get this sample working you need to do following:
- Unzip everything to a new, empty, directory. E.g. c:\magic\internet\sample\fileupload
- add an alias "FileUpload" (without quotes) to your internet server (IIS or apache) configuration. This alias (virtual directory) needs to point to c:\magic\internet\sample\fileupload\htdocs.
- Ensure that your Internet server configuration has an (script) alias "Magic94Scripts" which needs to point to the Magic 9.4 scripts directory (A Magic 9.4 installation adds this alias by default. Probably you do not have to do anything)
- Adjust the magic.ini in the directory c:\magic\internet\sample\fileupload to your requirements.
- Add a logical name "AppPath" to the .ini and let it point to your installation (c:\magic\internet\sample\fileupload\).
- Put the two DLLs mgtools.dll and mgtoolsh.dll in your Magic directory (where mgrntw.exe is). This two DLLs are not required for upload functionality. The sample uses them to create secure session IDs and create directories.

Basically thats it. When finished you need to start your broker and an application server with the .ini you just adjusted to your requirements.
Afterwards you point your browser to http://localhost/FileUpload. This should load the default.htm and come up with a login page. The required users you need to put manually in the DB. I used Pervasive tables for it. One user is pre-configered.
username: test
password: test

Let me know if you experience problems with getting this running. The sample was ONLY tested with MS IIS 6. If you want to get this running with Apache you need to change few things.

Important note!

By not restricting the maximum filesize of a file a user can upload, you basically allow a possible DOS (denaial of service) attack since your server gets quite busy when multiple, huge, files arrive. You can avoid this by setting the MaxUploadKB value in your mgreq.ini. You should also rather refrain from allowing anonymous users from uploading files (if you don not implement a REALLY PUBLIC website) and want to be surprised what you find on your hard-disk few days later

Further note: This is not a production code application. Its a sample and you can use AS IS, ...



Further readings / see also:
- Sending Files from Magic & Internet application to a browser thru Requester
- v10 documetation on "Upload capability of the Requester" which has some information regarding this issue
- RFC 1867 - Form-based File Upload in HTML



CategoryWeb

There are 4 comments on this page. [Display comments]

Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by Wikka Wakka Wiki trunk
Page was generated in 0.0654 seconds