22) Using Memory Loading. |
The following code uses a number of techniques to place files on a server. Any MIME type is being allowed and blank file fields are not in this case. It is assumed the user can upload as many files as they wish from some kind of dynamic script on the client and that with each file uploaded comes an ordinary text parameter that is telling the server which 'project' to place the file into. The Xload code below will memory load all the files and then ascertain if a file already exists in the appropriate project directory with the same name. If it does the file is not transferred and a report is generated, otherwise the file is transferred and therefore successfully uploaded. This code assumes that project names are sent with each file uploaded and that they are valid.
XloadManager xman = new XloadManager(request); xman.targetAll(4096); xman.upload();
//deal with any failed StringBuffer error = new StringBuffer(100); List failed = xman.getFailedFileUploads(); it = failed.iterator(); while(it.hasMore()){ upload = (XloadFileUpload)it.next(); String param = upload.getRequestParameter(); error.append(param + " failed to upload due to:" + //list possible reasons or give generic //message. "\n"); }
//handle successful uploads StringBuffer report = new StringBuffer(100); List successful = xman.getSuccessfulFileUploads(); XloadFileUpload upload = null; Iterator it = successful.iterator(); while(it.hasMore()){ upload = (XloadFileUpload)it.next(); String fileParam = upload.getRequestParameter(); //access ordinary parameter. String projectName = xman.getParameter(fileParam + "projectname"); XloadFile file = upload.getMemoryFile(); //access project directory. XloadDirectory dir = xman.getDirectory("projects/" + projectName); if(dir.fileExists(file.getRemoteName()){ report.append("The file named: " + file.getRemoteName() + " already exists in the Project named: " + projectName + "\n"); }else{ XloadFile diskFile = file.moveTo(dir); //place diskFile details inside relational database. } }
//return a response using the error object and the report //object.
|
As you can see this is quite a complex scenario and takes a small amount of code and complexity to implement. Obviously, this specification could be enhanced to cope with even more complex requirements.
Notice the use of the targetAll() method allowing targeting for file uploads that the server does not know about. Also, notice the moveTo() method which, in the case of a memory loaded file deployment returns a new disk based file deployment (which inherits the memory loaded file deployments state).
© Gubutech(Xload) 2006 (v1.2)