Upload the plug-in dropzone. js instance and the plug-in dropzone. js instance.

Source: Internet
Author: User

Upload the plug-in dropzone. js instance and the plug-in dropzone. js instance.

Dropzone. js uploads images to the server via Ajax by default. How can I get the image name? In fact, we can use the success function of dropzone to obtain the data returned by the server.

The configuration of dropzone. js in HTML is as follows;

Dropzone. autoDiscover = false; // prevent "Dropzone already attached. "Error $ (". dropzone "). dropzone ({url: "_ URL _/upload/", addRemoveLinks: true, dictRemoveLinks: "x", dictCancelUpload: "x", paramName: "userImg", maxFiles: 10, maxFilesize: 5, acceptedFiles: "image/*", init: function () {// res is the data returned by the server. this. on ("success", function (file, res) {// convert a json string to a json object var obj = JSON. parse (res); // res is dropzone. j S returns the image path by file. path = res; if (obj. status = 200) {// generate a hidden domain for the server's data. Var input = '<input type = "hidden" name = "' + obj. details. savename + '"value ="' + obj. details. savepath + obj. details. savename + '"/>'; $ ('. myform '). append (input) ;}else {alert ('upload failed') ;}}); this. on ("removedfile", function (file) {$. ajax ({url: "change to the path of your php image deletion", type: "post", // file. path can obtain the image data: {'path': file. path }});});}});
The PHP code is as follows (Thinkphp code ):
Public function upload () {/* add product: product name, product image * // instantiate the upload class $ Upload = new \ Think \ upload (); // set the attachment upload size $ upload-> maxSize = 3145728; // set the attachment upload type $ upload-> exts = array ('jpg ', 'gif', 'png ', 'jpeg '); // A developer writes upload () B Development // sets the attachment upload directory $ upload-> savePath = '. /Public/Uploads/'; // return the upload information $ info = $ upload-> uploadOne ($ _ FILES ['userimg']); // dump ($ info ); exit; if (! $ Info) {// upload error message/$ this-> error ($ upload-> getError (); $ data ['status'] = 404; // error message $ data ['msg '] = $ upload-> getError (); echo json_encode ($ data );} else {// uploaded successfully (image path and image name) $ data ['status'] = 200; $ data ['msg '] = 'upload SUCCESS '; // original image name $ data ['details'] ['originname'] = $ info ['name']; $ data ['details'] ['savename'] = $ info ['savename']; $ data ['details'] ['savepath'] = $ info ['savepath']; echo json_encode ($ data );}}

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

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.