Example of dragging and uploading files using Dropzone. js

Source: Internet
Author: User
Tags event listener file size file upload html form jquery library

Dropzone. js is an open-source JavaScript library that provides the AJAX asynchronous file upload function. It supports drag and drop, maximum file size, setting file types, previewing Upload results, and does not rely on the jQuery library.


Use Dropzone
We can create a formal form for uploading the form and give the form A. dropzone class.

<Form id = "mydropzone" action = "/upload. php" class = "dropzone"> </form>

In this way, the Dropzone will automatically find. the form element of dropzone, and the program that uploads to the background to receive files through the action attribute, such as upload. php is like accepting a very common file input form:

<Input type = "file" name = "file"/>

Then, in your upload. php writes the upload code. The Dropzone official website only downloads js code without background Upload code. However, helloweba.com provides you with the php version of complete Upload instance code. Please download the source code.
The next step is to introduce dropzone. js.

<Script src = "dropzone. min. js"> </script>

Then you don't need to do anything. Open the browser and test the drag-and-drop Upload effect. Of course, you can write the style yourself or refer to our instance code.
In another case, we do not want to upload a form in the html file. Therefore, we only need to place a div # mydropzone in the html file.
<Div id = "mydropzone" class = "dropzone"> </div>

Then, configure the js call:
Var myDropzone = new Dropzone ("div # mydropzone", {url: "upload. php "});

If you are using jquery, the jQuery version can be called as follows:
$ ("# Dropz"). dropzone ({
Url: "upload. php"
})

Run your webpage to see the upload effect.
Configure Dropzone

Dropzone is very flexible and provides many options and events. Below are several common configuration items in Dropzone.
Url: The most important parameter that specifies the page on which the file is submitted.
Method: The default value is post. If necessary, you can change it to put.
ParamName: equivalent to the name attribute of the <input> element. The default value is file.
MaxFilesize: maximum file size, in MB.
MaxFiles: The default value is null. It can be specified as a numerical value to limit the maximum number of files.
AddRemoveLinks: The default value is false. If it is set to true, a delete link is added to the file.
AcceptedFiles: specifies the file type that can be uploaded. The format is a comma-separated MIME type or extension. For example: image/*, application/pdf,. psd,. obj
UploadMultiple: indicates whether to allow Dropzone to submit multiple files at a time. The default value is false. If it is set to true, it is equivalent to adding the multiple attribute to the HTML form.
Headers: If set, it will be sent to the server as an additional header. Example: {"custom-header": "value "}
Init: a function called during Dropzone initialization. It can be used to add its own event listener.
ForceFallback: Fallback is a mechanism that provides an alternative solution when the browser does not support this plug-in. The default value is false. If it is set to true, fallback is forced.
Fallback: a function that is called if the browser does not support this plug-in.

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.