In php, how does one implement asynchronous file upload code instances through Ajax. 1: Get file object 2: read binary data 3: simulate an http request and send the data out (which is usually troublesome here). in forefox, use the sendasbinary method of the xmlhttprequest object to send 1: get
Based on springmvc, This article uses the annotation method to explain the examples from configuration to simple common functions. The functions have been tested on the local machine and can run.
References: iteye blog;
Spring document;
1. Import relevant jar packages:
Spring-core-3.2.0.jar; spring-web-3.2.0.jar; spring-webmvc-3.2.0.jar; common-annotations.jar; commons-fileupload-1.2.2.jar.
2. In the web. xml configuration dispatcherservlet, intercept matching requests; (the default WEB-INF fold
Uploading Files using form formKnowledge points/attention pointsFrom the form to add this line enctype="multipart/form-data" upload file upload is binary data with request. FILES got a dictionary, too.Front-end Code index.html"en">"UTF-8"> "/index/"Method="Post"Enctype="Multipart/form-data"> "text"Name="User">"file"Nam
jquery plug-in ajaxfileupload can implement Ajax file upload, the plug-in is very simple to use, first understand the correct use of the Ajaxfileupload plug-in method, and then learn some common error messages and solutions.
Instructions for use
Need to use jquery library files and ajaxfileupload library files
Working with instances
One, including the
1 varFileobj = document.getElementById (' file '). files[0];2 //js Get File Object3 varFilecontroller =URL;4 //receive the background address of the uploaded file5 6 //FormData Object7 varform =NewFormData ();8Form.append ("Author", "Test");9 //you can increase the form dataTenForm.append ("File", fileobj); One //
Front deskvar formData = new FormData (); var file = document.getElementById (' MyFile '). Files[0]; Formdata.append (' myFile ', file); var xhr = new XMLHttpRequest (); Xhr.open (' Post ', '/upload ', true); xhr.upload.onprogress = function (e) { if (e.lengthcomputable) { var percentage = (e.loaded/e.total) *; $ (' d
Recently I have written many things that have been written by others.
HtmlView code
PHPView code
move_uploaded_file($_FILES["fileField"]["tmp_name"], "upload/" . $_FILES["fileField"]["name"]);echo "upload/" . $_FILES["fileField"]["name"];?>
JS
/*Author: TianyaQQ: 63886829Email: flyinksy@gmail.com(Ajax File
Use the Layui framework to upload files to debug and intercept successfully! [HttpPost] Public Iactionresult Method1 (iformfile file) { return Json (new{success= "OK"}); Original ASP. NET Core 2.1.2 Test successful Ajax upload file
Here about the servlet how to upload the file without saying, how will get the percentage of file data already uploaded ...
First we write a class this class to implement Progresslistener this interface, implement the inside of the update (...) Method. The code is as follows
Package com.test.servlet;
Import Java.text.DecimalFormat;
Import Javax.servlet.http.H
next intercept start = end;End = start + num;Because the interception may exceed totalsize, determine whether the last interception is greater than totalsize if it is greater than the direct end equals to TotalSizeif (end>totalsize) {end = TotalSize;}You can use this as a progress bar.Loaded ((start/totalsize) *100);Recursive, if the file is not intercepted, continue to intercept WriteFile ();}}; Xhr.send (FormData);};
Call Wr
The code is divided into the following sections:
demo.php-Upload process processing
PLAIN textphp:
Include ' UploadProgressMeter.class.php ';
$fileWidget = new Uploadprogressmeter ();
if ($fileWidget->uploadcomplete ()) {Upload finished, from the IFRAME sent a JS to the main window to indicate everything OKecho $fileWidget->finalstatus ();
Process the uploaded f
acquisition and display progress, I used a self-written Ajax application architecture, extracted part of the code from it, note that JS files only apply to the UTF-8 coding page, because now I only write UTF-8 coding page, International convenience.
The following is an example.
The source code is provided below:
Process uploaded files
Example
Progressupload. jar contains the commons-fileupload-1.1.1.jar, servlet-api.jar, commons-io-1.2.jar, in pr
This article mainly describes jquery using Ajaxsubmit () Submission Form example, using the third-party plug-in jquery.form implementation, the need for friends can refer to the next,The Ajaxsubmit (obj) method is a plug-in jquery.form.js inside the jquery method, so using this method requires first introducing this plugin. As shown below:So, how do you submit data through Ajaxsubmit (obj)? First we need a form:XhtmlTitle: Content: The above is a form that needs to submit content, usually, we su
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.