Get the App
SLTechnology News&Howtos  ›  Development  › 

How does javascript construct a form form that can upload files?

Shulou Source: shulou.com Published: 2022-06-02 08:20:44 09月29日 Update

This article mainly introduces "javascript how to construct form forms that can upload files". In daily operation, I believe many people have doubts about how javascript constructs form forms that can upload files. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "how javascript constructs form forms that can upload files". Next, please follow the editor to study!

When uploading files, we all know that we need to add enctype= "multipart/form-data" to the form form. During the development process, you may also encounter situations where you need to construct form forms with javascripts. For general javascript to construct form, you can use the following methods:

Var form = document.createElement ('form')

Form.id= "the_form"

Form.action= "/ img.php"

Form.method='post'

Form.name= "the_form_"

When the form constructed by javascript needs to upload files, it thought that adding a form.enctype= "multipart/form-data" directly would solve the problem, but found that this was not the case. Search for some reasons from the Internet, and finally find the solution. The original javascript form object name should not be enctype, but should be encding, instead of form.encding= "multipart/form-data", the problem is solved.

You can use the following code to place the form in a HTML tag:

Var parNode = document.getElementById ("plus")

ParNode.appendChild (form); / / add form form to plus, and plus is a layer in html

You can also put input, checkbox, and other forms in the form. You can use the following code to change the type and value of the form through its object:

Var input=document.createElement ("input")

Input.id= "upfile_1"

Input.name= "upfile_1"

Input.type= "file"

Input.className= "file_upload"

The upload_pic () function is called when input.onchange=function () {/ / clicks on the form

Upload_pic ()

}

Form.appendChild (input); / / append input form to form

At this point, the study on "how to construct javascript form forms that can upload files" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Forms files learning problems code reasons you can make objects methods more for help practical and then functions things situations articles times tones Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS NVidia Shulou Tech Info Shulou Information Redmi