Forum Discussion

Dave Walker's avatar
Dave Walker
Copper Contributor
Jan 22, 2020

SharePoint Framework creatRef object undefined

Hi, I'm a newbie to React and the SharePoint framework and I'm hoping someone can help me with the same issue as this posting --> https://github.com/OfficeDev/office-ui-fabric-react/issues/4733.  Basically I am creating a webpart that needs to upload a file to a document library.  The office-ui-fabric-react button control does not have this capability built-in, but the HTMLInputElement does.  The solution by Je-T in the above link describes using a UI fabric button to pass the event to a reference that has been created for the HTMLInput element. Makes sense to me, but it isn't working for me in practice.  Basically the fileUploadRef reference is always undefined int he ClickUpload methods.  can anyone help me understand the error the below code.  if you have a working example of something similar then that would help as well.

 

import * as React from "react";
import { IPhotoUploaderProps } from "./IPhotoUploaderProps";
import styles from "./PhotoUploader.module.scss";
import { createRefIButtonStylesIconButtonHighContrastSelectorLabelIContextualMenuPropsIIconPropsPrimaryButtonIButtonProps } from 'office-ui-fabric-react';
import Dropzone from 'react-dropzone';
import { ModalILayerPropsfrom 'office-ui-fabric-react';


export class PhotoUploader extends React.Component<IPhotoUploaderProps> {
    constructor(propsIPhotoUploaderProps) {
        super(props);
      
    
    }

    private fileUploadRef = createRef<HTMLInputElement>();

    private onDrop = (acceptedFiles=> {
        console.log(acceptedFiles);
    
    }

    private layerPropsILayerProps = {eventBubblingEnabled: true};

    private clickUpload() {
        if (this.fileUploadRef.current) {
            this.fileUploadRef.current.click();
        }
    }

    private uploadFile(clickEventEventInit) {

    }


    public render(): React.ReactElement<IPhotoUploaderProps> {
        const cancelIconIIconProps = { iconName: 'Cancel'};
     
        return (
            <Modal isOpen={this.props.showModal} onDismiss={this.props.modalCancel}  layerProps={this.layerProps} >  
            <div className={styles.photoUploaderContainer}>
                 <IconButton 
                      iconProps={cancelIcon}
                      onClick={this.props.modalCancel}
                      className={styles.cancelButton}
                 />
                <div className={styles.heading}>Submit a Photo</div>
                <p>
                    You can submit home page banner photos to be seen by <strong>all users company-wide.</strong> All photos are reviewed before
                    use.  If your photo is chosen, your name will appears in the attribution on the home page.
                </p>
                <p>
                    <strong>Do not submit photos you don't want the whole company to see!</strong>
                </p>
        
               <PrimaryButton onClick={this.clickUpload} title="Upload File">Upload Image</PrimaryButton>
               <input ref={this.fileUploadRef} type="file" accept="application/xml" onChange={this.uploadFile}  />

            </div>
           </Modal>
        );
    }
}

 

Resources