Modify order of form fields in upload request

When uploading to Amazon S3 using a post policy it requires that "key" be the first form field. How can I configure my request so that the uploader file form field is last in the request? Please see photo.

I'm using uploading event to configure the custom request data before each upload, but I can't configure order of the Upload components file in the request.

    onUploading(argUploadingEventArgs | undefined): void {
        if (arg === undefinedreturn;
        
        let formDatas = new Array<{ [keystring]: Object }>(
            { 'key': this.uploadConfig.keyPath + arg.fileData.name },
            { 'AWSAccessKeyId': this.uploadConfig.accessKeyId },
            { 'acl': this.uploadConfig.acl },
            { 'x-amz-meta-uuid': this.uploadConfig.directoryUuid },
            { 'Content-Type': this.uploadConfig.contentType },
            { 'Policy': this.uploadConfig.policy },
            { 'Signature': this.uploadConfig.signature }
        );
        for (let i = 0i < formDatas.lengthi++) {
            arg.customFormData.push(formDatas[i]);
        }
    }


Capture.PNG


7 Replies 1 reply marked as answer

BC Berly Christopher Syncfusion Team June 22, 2021 02:39 PM UTC

Hi Nick, 
  
Greetings from Syncfusion support. 
  
We have checked the requested requirement. We could not change the order of request based on the certain condition on the files which you selected since we have formed the request from the source level. This is the default behavior of the component. 
  
So, we have overridden the source method named “uploadFilesRequest” to append the custom form data after original form data in the request. Kindly refer the below sample below. 
  
  
Screenshot: 
 
  
Regards, 
Berly B.C 



NL Nick Lechnowskyj June 23, 2021 02:02 AM UTC

Hi, thank you.


I am having an issue in Typescript where the uploadFilesRequest overload is not being invoked. My onCreated function is being invoked though.


onCreated() {
        (Uploader.prototype as any).uploadFilesRequest = function (selectedFilesanyianycustomany) {
            var _this = this;
            var cloneFilesany = [];
            var chunkEnabled = this.checkChunkUpload();
            var ajax = new Ajax(this.asyncSettings.saveUrl'POST'trueundefined);
            ajax.emitError = false;
            var getFileData;
            /* istanbul ignore next */
            if (this.isServerBlazor) {
                getFileData = selectedFiles.slice(0);
                cloneFiles.push(getFileData[i].rawFile);
            }
            var eventArgs = {
                fileData: this.isServerBlazor ? getFileData[i] : selectedFiles[i],
                customFormData: [],
                cancel: false,
            };
            var formData = new FormData();
            ajax.beforeSend = function (eany) {
                (eventArgs as any).currentRequest = ajax.httpRequest;
                /* istanbul ignore next */
                if (isBlazor()) {
                    eventArgs.fileData.rawFile = !chunkEnabled
                        ? _this.base64String[i]
                        : eventArgs.fileData.rawFile;
                    if (_this.currentRequestHeader) {
                        _this.updateCustomheader(
                            ajax.httpRequest,
                            _this.currentRequestHeader
                        );
                    }
                    if (_this.customFormDatas) {
                        _this.updateFormData(formData_this.customFormDatas);
                    }
                }
            };
            if (selectedFiles[i].statusCode === '1') {
                _this.trigger('uploading'eventArgsfunction (eventArgsany) {
                    /* istanbul ignore next */
                    if (_this.isServerBlazor && !chunkEnabled) {
                        selectedFiles[i].rawFile = eventArgs.fileData.rawFile =
                            cloneFiles[i];
                    }
                    if (eventArgs.cancel) {
                        _this.eventCancelByArgs({}, eventArgsselectedFiles[i]);
                    }
                    _this.updateFormData(formDataeventArgs.customFormData);
                });
                var name_5 = this.element.getAttribute('name');
                formData.append(
                    name_5,
                    selectedFiles[i].rawFile,
                    selectedFiles[i].name
                );
                if (
                    chunkEnabled &&
                    selectedFiles[i].size > this.asyncSettings.chunkSize
                ) {
                    this.chunkUpload(selectedFiles[i], customi);
                } else {
                    ajax.onLoad = function (e) {
                        if (eventArgs.cancel && _this.isServerBlazor) {
                            return {};
                        } else {
                            _this.uploadComplete(eselectedFiles[i], custom);
                            return {};
                        }
                    };
                    ajax.onUploadProgress = function (eany) {
                        if (eventArgs.cancel && _this.isServerBlazor) {
                            return {};
                        } else {
                            _this.uploadInProgress(eselectedFiles[i], customajax);
                            return {};
                        }
                    };
                    /* istanbul ignore next */
                    ajax.onError = function (eany) {
                        _this.uploadFailed(eselectedFiles[i]);
                        return {};
                    };
                    ajax.send(formData);
                }
            }
        };
    }

    componentDidMount() {

        setTimeout(() => {
            this.rendereComplete();
        });
    }

    rendereComplete() {
        //this.uploadObj.dropArea = this.dropContainerEle;
        this.uploadObj.element.setAttribute('name''file');
        this.uploadObj.dataBind();
    }

    public render(): JSX.Element {
        return (
            <div id='dropArea' onClick={this.dropAreaClick = this.dropAreaClick.bind(this)} ref={dropEle => { this.dropAreaRef = dropEle! }}>
                <UploaderComponent id='file'
                    ref={upload => { this.uploadObj = upload! }}
                    type={"file"}
                    asyncSettings={this.path}
                    uploading={this.onUploading = this.onUploading.bind(this)}
                    progress={this.onFileUpload = this.onFileUpload.bind(this)}
                    selected={this.onSelect = this.onSelect.bind(this)}
                    success={this.onuploadSuccess = this.onuploadSuccess.bind(this)}
                    failure={this.onuploadFailed = this.onuploadFailed.bind(this)}
                    template={this.uploaderTemplate = this.uploaderTemplate.bind(this)}
                    created={this.onCreated.bind(this)} />
            </div>);


BC Berly Christopher Syncfusion Team June 23, 2021 08:30 AM UTC

Hi Nick, 
  
As per the requirement, we have prepared the sample in TSX and source overridden method will be fired on file selection correctly. Kindly refer the below screenshot.  
  
 
  
  
Regards, 
Berly B.C 


Marked as answer

NL Nick Lechnowskyj replied to Berly Christopher June 24, 2021 12:56 AM UTC

Hi,


When I compile I get the following error:

Property 'uploadFilesRequest' is private and only accessible within class 'Uploader'.



PO Prince Oliver Syncfusion Team June 24, 2021 07:07 AM UTC

Hi Nick, 

Thank you for your update. 

The mentioned error “Property 'uploadFilesRequest' is private and only accessible within class 'Uploader'.” is typing error. Since we need to access and override the components private methods in order to achieve you requirement. We recommend you to cast any such properties to “any” type. 

(Uploader.prototype as any).uploadFilesRequest 

Let us know if you need any further assistance. 

Regards, 
Prince 



NL Nick Lechnowskyj June 24, 2021 12:51 PM UTC

Hi,

Using this.uploadObj worked for me.

(this.uploadObj as any).uploadFilesRequest = function(selectedFilesicustom)


BC Berly Christopher Syncfusion Team June 24, 2021 02:34 PM UTC

Hi Nick, 
  
We are glad to know that the issue is resolved at your end. Please let us know if you need further assistance on this. 
  
Regards, 
Berly B.C 


Loader.
Up arrow icon