Registering a new file with a post, deleting the existing files

Home > Basic examples > Example 07

Explanation

A file that has already been uploaded can be represented using a virtual file, and the unique key of the file is registered using the vindex property.

That is, deleting an existing file means deleting the virtual file.

This is the same as the "Transferring files and form data" example before, but there is a difference in that you need to pass along the deleted virtual file information at the time of the submit.

function submit(response) {
	var formObj = document.getElementsByTagName("form")[0];
		
	formObj.newFileKeys.value = response || "";
	
	// Getting the deleted virtual file array.	
	var arr = dx5.get("dext5").getRemovedFiles();  
	
	// Creating a string of the form vindex;vindex; ... and register it.
	var deleted = arr.map(function(v) { return v.vindex; }).join(";");  
	
	formObj.deleteFileKeys.value = deleted || "";
	
	formObj.submit();
}

When processing the form data on the server side, it is good to handle the deleted file.

@RequestMapping(value = "/service/form-process.do", method = RequestMethod.POST)
public String formprocess(..., @RequestParam(value = "deleteFileKeys", required = false) String deleteFileKeys, ...) {

	if (deleteFileKeys != null) {
		// In the example, we simply set whether to delete the file, 
		// but normally we need to delete the target file information from the associated DB file table.
	}
	...	
	if (deleteFileKeys != null) {
		// The process of deleting the physical file uploaded to the real server is safe at the end of every transaction.
	}
	...
}.

Example