Three ways to upload files asynchronously in Java

This article shares three Java asynchronous file upload methods for your reference. The specific contents are as follows

The first is to upload using browser plug-ins, which requires a certain basic coding skills. I won't talk about it here to avoid harming people's children. You can baidu on your own. The second uses hidden iframe to simulate asynchronous upload. Why are we talking about simulation here? Because we actually put the returned results in a hidden iframe, we didn't jump the current page. It feels like an asynchronous operation.

This technology has two key points: 1 The form specifies the target, and the submitted results are directed back to the hidden ifram. (that is, the target of the form is consistent with the name attribute of iframe). 2. After submission, the page in iframe communicates with the main page to inform the upload results and how the server file information communicates with the main page? We use nodejs to return a window after receiving the file parent. The method defined on the main page. After execution, you can know that the file upload is completed. The code is simple:

After execution, you can open the developer option, and you will find that some data of the server is returned in the hidden iframe. The third uses xmlhttprequest2 for real asynchronous uploads. Post the code first: you can open the developer option after execution, and you will find that some data returned by the server in the hidden iframe. The third uses xmlhttprequest2 for real asynchronous uploads. Or post the code first:

A little bit of code, but easy to understand. Anyone who has used Ajax knows that the XHR object provides a callback method of onreadystatechange to listen to the whole request / response process. There are several more progress events in the XMLHttpRequest Level 2 specification. There are six events:

1. Loadstart: triggered when the first byte of response data is received. 2. Progress: triggered continuously during receiving the response. 3. Error: triggered when an error occurs in the request. 4. Abort: triggered when the connection is terminated because the abort method is called. 5. Load: triggered when complete response data is received. 6. Load: triggered after the communication is completed or the error, abort and load events are triggered.

This time we can interpret the code: when the transmission event starts, we add a click event on the stop transmission button, and the abort method is built to stop the transmission. If it is not clicked, it will be uploaded normally until the transmission is completed. Its background code is similar to the second method. Each of the three methods has its own advantages and disadvantages. Make a simple summary. Third party controls have good interactivity and controllability, because they are close to the bottom, and their performance is also very excellent. However, due to the difficulty of writing, you usually need to install the plug-in yourself, and sometimes you may need to write it yourself. I personally think the hidden iframe method is a very thoughtful method. Iframe can help us do a lot of things. This approach has extensive browser compatibility and does not require the installation of plug-ins. However, its interactivity is poor, the upload process is uncontrollable, and its performance is very general. Xhr2 pure Ajax upload, it must be a higher version of the browser (IE9 +). But it is very interactive. You can see the upload progress and it is controllable. Development can adopt different methods according to needs. Personally, I think these file uploads, especially the second one, provide an idea and make full use of the characteristics of some HTML tags, which may be where our developers need to think more.

The above is the whole content of this article. I hope it will be helpful to your study.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>