Jfinal cannot get form parameters when uploading files

If the upload file form form uses enctype="multipart/form-data", the getFile series method must be called first to make the getPara series method work properly.

Because the upload control of layui is used to upload files through Ajax, and FormData is used for Ajax requests, not enctype="multipart/form-data".

Therefore, file upload requests are uniformly changed to use FormData for Ajax request submission, and there is no need to consider whether to call the getFile series of methods first.

 

However, when uploading files using the wx.uploadFile interface of the WeChat applet, you need to call the getFile series of methods before you can get other form parameters.

This request method cannot be changed. It can only call the getFile series of methods in the background first.

 

At present, there is a problem that the system's permission check interceptor obtains the token judgment permission. When uploading files through the wx.uploadFile interface, the token cannot be obtained, which causes the permission check to fail.

A quick and simple solution is to call the getFile series of methods directly in the interceptor, and then the form parameters can be obtained normally.

The disadvantage of this scheme: The permission check interceptor will intercept all requests and call the getFile series of methods, which will affect performance. After all, most of the requests intercepted by the system are not file uploads. In addition, no file is uploaded, so I am not sure if there will be other exceptions using the getFile series of methods.

Another solution:

The request that needs to call the getFile series of methods first, unify its URI to end with the specified character. Such as "upload". If the url has ended with upload, call the getFile series of methods first.

private static final String UPLOAD_ACTIONKEY_SUFFIX = "upload";
/**
	 * actionKey以upload结尾,则该操作包含上传文件,则需要先调用controller.getFiles();
	 * @param actionKey 例子 /dangan/uploadPhoto/upload 或者 /dangan/uploadPhoto/XXXXupload
	 * @param controller
	 */
	private void uploadFileUrlDeal(String actionKey, Controller controller) {
		if(actionKey.length() >= UPLOAD_ACTIONKEY_SUFFIX.length()){
			String endStr = actionKey.substring(actionKey.length() - UPLOAD_ACTIONKEY_SUFFIX.length(), actionKey.length());
			if(UPLOAD_ACTIONKEY_SUFFIX.equals(endStr) || UPLOAD_ACTIONKEY_SUFFIX.equals(StrKit.firstCharToLowerCase(endStr))){
				controller.getFiles();
			}
		}
	}

public void intercept(Invocation inv) {
		Controller controller= inv.getController();
        HttpSession session = inv.getController().getSession();  
    	String actionKey = inv.getActionKey();
    	uploadFileUrlDeal(actionKey,controller);
        String token = controller.getPara(TokenUtil.TOKEN_STR);
// ....
}

 

Guess you like

Origin blog.csdn.net/xiaozaq/article/details/89021363