Different field name for asynchronous upload and full form file field.

Different field name for asynchronous upload and full form file field.

bblumedtrbblumedtr Posts: 13Questions: 6Answers: 0
edited May 2022 in Editor

I have two tables with two APIs.

Order:
- customer_name (text)
- customer_file (ForeignKey reference to an Uploaded_file's primary key)

Uploaded_file:
- file_object (file upload field)

I use an upload field with an AJAX object to upload the file to the Uploaded_file table:

{ 
                label: "file_object",
                name: "file_object",
                 type: "upload",
                 display: function ( file_id ) {
                      console.log("File ID: " + file_id)
                      return '<img src="'+editor.file( 'file_object', file_id ).web_path+'"/>';
                  },
                  clearText: "Clear",
                  noImageText: 'No image',
                  ajax: {
                    'url': "/api/Uploaded_file_rest/" 
                    headers: { 'X-CSRFToken': ... }
                  }
}

This allows me to upload the file asynchronously. I can see in my DB that the Uploaded_file table has added an appropriate row. However, when I submit the full form, I get an error. It's trying to write the PK returned for the Uploaded_file to the Order's field name file_object which doesn't work, because it's called customer_file in the order table.

Is there a way to use a different field name for the uploaded_file table and the Order table?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin
    Answer ✓

    If I'm understanding correctly, are you using the uploadField parameter in your /api/Uploaded_file_rest/ controller? That is the field name that Editor uses to tell the server what field is being uploaded for the upload action.

    If correct, then change the name parameter for the object to what you want the main form to submit (customer_file in this case) and use preUpload to modify the data being sent as part of the upload action (specifically set the uploadField parameter's value on the ajaxData parameter).

    Regards,
    Allan

  • bblumedtrbblumedtr Posts: 13Questions: 6Answers: 0

    Thanks, Allan. I had a workaround that was good enough for the time being and didn't want to muck with it until I had to. I came back to this to solve a different issue I was having. Works great!

Sign In or Register to comment.