Javascript abstract -- assign a value to the name attribute of the file tag

Source: Internet
Author: User

It has been a long time since javascript was not used as a page. On this day, a friend suddenly asked me about the FileUpload assignment problem, because after FileUpload is compiled, the <input type = "file"/> tag is generated. In the HTML document, <input type = "file"/>
The tag is used by the browser to send the selected file to the server. This element has a value
Attribute, stores the name of the file specified by the user. For security reasons, the value Attribute of the file tag is read-only and cannot be modified by programmers.
The property is also ignored. Therefore, when the database records the address of the file on the client and needs to assign values to the value attribute of the file tag, the problem may occur.

After a multi-flip query, we decided to use the "Hide tag" method to implement these functions. First, add a text tag, a button tag, and a file tag on the page, and set the sytle attribute of the file tag to "visibility: hidden; "simulate the appearance of the text tag and the button tag. In the window. onload event, the existing file path is displayed on the text tag. In The onclick method of the button tag, the file tag click event is triggered to obtain the uploaded file, and the path of the uploaded file is displayed on the text tag.

<Html xmlns = "http://www.w3.org/1999/xhtml"> 

Note that the above method only applies to assigning values to the name attribute of the file tag, but cannot directly obtain the file Upload file. This is because of security. the browser will prevent the file from being uploaded directly by binding the file tag through the program. If you want to directly upload files by hiding tags, it takes a little time. First, create a file tag, add a text tag to overwrite the text of the file, and assign a value to the text tag in the onchange event of the file.

<Html xmlns = "http://www.w3.org/1999/xhtml"> 

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.