5. Handling upload failure specifically, giving the user fine grained results as to why uploads have failed. Partial uploads ARE allowed.

 

back to examples

 

All MIME types are being allowed as is an empty field for a file parameter. The code informs the user of any failure to upload any file and it gives the user detailed reasons for upload failure. As can be seen it is a relatively small piece of code to achieve these fine grained results. Partial uploads are being allowed here so the code will still deal with any successful uploads even if some severe IO Error occurs part way through uploading. A response is sent if a partial upload has occurred as the IO Error may have not involved the client/server connection.

 

 

 

XloadManager xman = new XloadManager(request);

xman.target("file1", "uploaded1", 4096);

xman.target("file2", "uploaded2", 4096);

xman.target("file3", "uploaded3", 4096);

xman.upload();

 

//handle successful uploads first

List successful = xman.getSuccessfulFileUploads();

XloadFileUpload upload = null;

Iterator it = successful.iterator();

while(it.hasMore()){

upload = (XloadFileUpload)it.next();

XloadFile file = upload.getFile(1);

//place file details inside relational database

}

 

//deal with failed

int fieldBlank = 0;

StringBuffer error = new StringBuffer(100);

List failed = xman.getFailedFileUploads();

it = failed.iterator();

while(it.hasMore()){

upload = (XloadFileUpload)it.next();

String param = upload.getRequestParameter();

switch(upload.getFailureCode()){

case 1:

fieldBlank++;

break;

case 2:

error.append(param + " cannot be uploaded because " +

"the path given does not represent a file.\n");

break;

case 4:

error.append(param + " cannot be uploaded because " +

"the file is too large(4Mb max).\n");

break;

case 5:

error.append(param + " cannot be uploaded because " +

"a serious server error has occurred.\n");

break;

default:

error.append("There has been a problem uploading " +

param + "\n");

//please note that case 3: will not occur as we are allowing all

//MIME types.

}

}

 

if(xman.partialUploadOccurred()){

error.append("Your upload was interrupted therefore some " +

"uploads may not have been attempted.");

//could add to this a list of all uploads that

//were not attempted using the

//getRedundantTargetUploads() method. Most partial uploads

//occur with a breakdown in the client/server connection,

//so be pragmatic as most of the time information gathered

//here will be unused as a response cannot be sent.

 

}else if(fieldBlank == 3){

//return a response displaying that there have been

//no files uploaded as all file fields are blank.

}

//return a response using the error object as an error message

//(i.e. some uploads have failed).

 

 

 

back to examples

 

 

 

 

 

© Gubutech(Xload) 2006 (v1.2)