No need to upload new files and return custom value _ javascript skills

Source: Internet
Author: User
This article briefly shares with you how to solve the problem of File Upload without refreshing in your personal projects. If you need it, you can refer to it. During the development process today, I encountered the following problem: I need to upload the Excel file to the server for parsing. However, if the document is inappropriate, I hope that the user document is inappropriate without refreshing a new page. After thinking for a long time, I found a lot of information on the Internet and finally succeeded in the experiment. Here I will share my solution:

First, let's talk about the solution: Add a hidden iframe on the page and set the target attribute of the form to the id of iframe, in this way, when the form is submitted, the excel file is uploaded to the background in the form of a file stream. After receiving the form in the background, you can perform custom operations. Then, the returned information is displayed in iframe without redirecting, iframe is previously set to hidden, so the page will not change. Then we need to listen to changes in the iframe content, and then pass the content into the JS method in the main window for the next step of custom processing.

The page code is as follows:

 

The JS Code is as follows (jqeury needs to be introduced ):

$ (Function () {$ ("# hiddenIFrame "). load (function () {var wnd = this. contentWindow; var str = callback (wnd.document.body).html (); callback (str) ;}) function callback (info) {alert (info );}

The background code is just like traditional submission. The background will obtain a file stream with the same name based on the name value of the input component (for example, the name of the input component in the code above is file, the background receives a file stream named file. After receiving the stream, you can perform custom operations.

Related Article

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.