Upload a file without refreshing and return a custom value

Source: Internet
Author: User

Upload a file without refreshing and return a custom value

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:

1

2

3

4

5

<Form id = "input" action = "importExcel. jhtml" method = "post" enctype = "multipart/form-data" target = "hiddenIFrame">

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

<Input type = "submit" class = "button" value = "Import excel"/>

</Form>

<Iframe id = 'hiddeniframe' name = 'hiddeniframe' style = "display: none;"> </iframe>

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

1

2

3

4

5

6

7

8

9

10

11

$ (Function (){

$ ("# HiddenIFrame"). load (function (){

Var wnd = this. contentWindow;

Var str = (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.

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.