SDK |
Documentation |
JavaSonics ListenUp is no longer for sale.Writing Server Scripts with ASPASP "classic" has no simple utility for receiving uploaded files. Most developers choose to use a 3rd-party component for this function. Most IIS web hosts provide some such component for server-side application use as part of their hosting package. One of the most common and well-supported such components is ASPUpload from Persits Software (http://www.aspupload.com/); we'll use that for our examples. Note that ASPUpload is a commercial product. You can find some free alternatives here. For a complete example used with ListenUp, please see the file "asp_test/handle_upload_simple.asp". Note that this file checks for specific file names that we use in our tests. You should modify or remove these checks to match your application's file names. Use Plain TextThe first thing you must do in your ASP script is to set the content type to plain text. This simplifies the text that is sent back to ListenUp, making it easier to parse. These header values must be set before you send any other text! response.ContentType = "text/plain" Instantiate the Upload objectYou need to create the object that will handle the file upload. Using ASPUpload, for example: Set upload = Server.CreateObject("Persits.Upload") Getting Variable Data (Name/Value pairs)The ASP Request object has a property, .Form, that is a collection of POST name/value pairs accompanying the request. You can use this to discover information sent along with the uploaded file. For example, to get the duration of the uploaded file in seconds: duration = strip_tags(Request.Form("duration")) strip_tags() is a function we've defined to use regular expressions to strip HTML from a string. You might use it as above to remove malicious or malformed parameters passed in with the form. ASPUpload has a property, UploadManager.Form, that does the same thing after you've called the Save method to parse and store the file. Using this property, the above example would be: upload.Save ' saves the file in memory duration = strip_tags(upload.Form("duration")) Getting Information about the Uploaded FileAfter you've called the Save method, you can retrieve specific information about the uploaded file by looking at the properties of the UploadedFile object in the UploadManager.Files collection. For example, to view the size and suggested name of a file called "userfile", you can use the following: Set file = upload.Files("userfile") upfile_size = strip_tags(file.Size) upfile_name = strip_tags(file.OriginalFileName) ValidationIt is possible that someone could create another program that pretended to be ListenUp and send you some bogus files. So you should check to make sure that noone is uploading a file that is too big. if upfile_size > upfile_size_limit then Response.Write "ERROR - file too large, " & upfile_size & " > " & upfile_size_limit & vbLF end if The UploadFile object has properties for the original name (no path), the original folder (just the parent path) and the original path (the whole enchilada). You're going to want to use .OriginalFileName to avoid users sending something unwanted in the path. upfile_name_safe = strip_tags(file.OriginalFileName) Save file to destinationSave the file to its final destination. IIS must have permission to write into that folder. upload_dir = "../uploads" file.SaveAs Server.MapPath(upload_dir) & "\" & upfile_name_safe ProblemsLarge FilesThe technique shown above saves the uploaded file first to memory. If the file is large, this may fail. In this event, you'll want to save the file to a temporary spot on your server disk, then use UploadFile.SaveAs or UploadFile.Move to move it to the final destination. PermissionsIt's quite common to run into a conflict between Windows and IIS permissions. If your application is set up to require authentication, the authentication method will determine the user account that is used to write an uploaded file: it may be the IIS_USER account, or the Windows account of the logged-in user. If the destination directory is not set up with the requisite access control, your call to .SaveAs or .Move may fail. Debugging this kind of problem can be somewhat painful. Start by setting authentication requirements for your ListenUp app to Anonymous (no authentication required), make sure the uploads directory has Read and Write permission for Everyone, and that IIS has Read and Write access to that directory. See http://www.15seconds.com/Issue/020123.htm for a decent overview of troubleshooting this kind of problem. Return Status CodesYour script must return a status code of SUCCESS, WARNING or ERROR at the beginning of a line. For example: Response.Write "SUCCESS - file uploaded." & vbLF Clean upMake sure you remove any references to objects you've created. For example, to remove references to ASPUpload objects: Set file = Nothing Set upload = Nothing |