JavaScript implements uploading files to the background for reception

小云云
Release: 2018-05-19 13:36:29
Original
4857 people have browsed it

In the plug-in management interface of WordPress background management, I want to add an ajax upload without refreshing.Let me talk about the idea first: To upload a file, you must obtain the data stream of the current file, and then pass The ajax post method is sent to the server for processing.

(1) How to obtain the data stream of the current file?

Answer: Append the file data in a variable through the object instantiated by FormData()

(2) How to obtain the data?

Answer: In the input form with type file, it comes with a files attribute.

HTML page sends a file upload request:

 '' 
Copy after login

The server processes the file data and generates the uploaded file:

$success = array('status' => 'sucess', 'code' => '1'); $error = array('status' => 'error', 'code' => '0'); if (!empty($_FILES)) { $file = $_FILES['my_file']; $new_file_dir = dirname(__FILE__) . '/images/' . $file['name']; @move_uploaded_file($file['tmp_name'], $new_file_dir); exit(json_encode($success)); } else { exit(json_encode($error)); }
Copy after login

Related recommendations:

JavaScript Introduction to the method of uploading files without refreshing them

The above is the detailed content of JavaScript implements uploading files to the background for reception. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!