JS File Upload

Source: Internet
Author: User
Tags export class

Here is the REACT implementation

Encapsulate the Import together IMPORT.JSX

1Import React from ' React ';2Import reactdom from ' React-dom ';3 class Import extends React.component {4 Constructor (props) {5 super (props);6          This. Bindhander ();7     };8 9 Bindhander () {Ten          This. Opendisk = This. Opendisk.bind ( This); One          This. readfilecontent = This. Readfilecontent.bind ( This); A     }; -  - Opendisk () { the          This. Fileupload.click (); -     }; -  - Readfilecontent (e) { +  Let -_self = This, +File = E.target.files[0]; A         if(file) { at  Let -Reader =NewFileReader (); - reader.readasarraybuffer (file); -Reader.onload =function () { -_self.props.uploadcallback (File.name,NewUint8array ( This. Result)); -             }; in}Else -Console.log (' ERROR '); to     }; +  - render () { the         return<input type= "File" hidden ref={(el) = { This. FileUpload = el}} onchange={ This. readfilecontent} accept= ". xml"/> *     } $ };Panax NotoginsengExportdefaultImport;

Referencing import in INDEX.JSX

1Import import from './import ';2Const CONTANTS = {3Error: ' ERROR ',4TIMEOUT: ' Load Time Out ',5Loaderror: ' Load error! ',6Urlcannotfind: ' Url can not find '7 }8 9 Export Class Test extends React.component {Ten Constructor (props) { One Super (props) A     }; -  - Uploadclick () { the          This. Import.opendisk () -     }; -  - Uploadhandler (name, content) { +G.loading (true); -  Let +UploadFile = { A Name:name, at filecontent:content -             }, -Request = { -Method: ' POST ', - headers: { -' Accept ': ' Application/json ', in"Content-type": "Application/json" -                 }, toCredentials: "Same-origin", + body:JSON.stringify (UploadFile) -             }; theFetch (' api/test/fileupload ', request). Then (response = { *             if(Response.ok) $                 returnResponse.text ();Panax Notoginseng             Else { -                 if(Response.Status = = 401) { theG.loading (false); +G.alert (true, { A title:contants. ERROR, the content:contants. TIMEOUT, +Type: ' I ', -Clicky: () ={console.log (contants). Loaderror)} $                     }); $                 } -                 if(Response.Status = = 400) { -G.loading (false); theG.alert (true, { - title:contants. ERROR,Wuyi content:contants. Urlcannotfind, theType: ' I ', -Clicky: () ={console.log (contants). Loaderror)} Wu                     }); -                 } About                 Else Throw NewError (response.statustext); $             }; -}). Then (Datastr = { -             if(Datastr = =NULL|| Datastr = = "') -                 return NULL; A             Else returnJson.parse (DATASTR); +         }) the     } -  $ render () { the         return<div> the<div onclick={ This. Uploadclick.bind ( This)}>{' Upload Click '}</div> the<Import theRef={el = This. import =El} -uploadcallback={ This. Uploadhandler.bind ( This)} in/> the</div> the     } About}

JS File Upload

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.