formio.js icon indicating copy to clipboard operation
formio.js copied to clipboard

Upload FILE to URL- key 'file' in formData to be customizable

Open Dhruvv4 opened this issue 7 years ago • 8 comments

Hi, We want to upload a file to our API where the key of 'file' in formData is different. Is there any way where we can change the key 'file' to be customizable? Partial code of the function is below.

uploadFile: function uploadFile(file, name, dir, progressCallback, url, options) {
      var uploadRequest = function uploadRequest(form) {
        return xhrRequest(url, name, {
          baseUrl: encodeURIComponent(formio.projectUrl),
          project: form ? form.project : '',
          form: form ? form._id : ''
        }, {
          file: file,
          name: name,
          dir: dir
        }

Thanks

Dhruvv4 avatar Sep 24 '19 08:09 Dhruvv4

I'm not entirely following what you are trying to do. The best thing to do is probably just to check the code in https://github.com/formio/formio.js/blob/master/src/providers/storage/url.js to see what is possible or not.

randallknutson avatar Sep 24 '19 14:09 randallknutson

@randallknutson Even I was looking for this change. because not all the upload REST API has the same name in form-data. For example, my REST API accepting name in form-data is resumes. But in current code, it seems hardcoded name file. https://github.com/formio/formio.js/blob/master/src/providers/storage/url.js#L86

Can we make this configurable in form builder side? like what we have for URL, Directory etc.

Inaamhusain avatar Sep 25 '19 10:09 Inaamhusain

It looks like that is currently hard coded to file. We'd be open to changing this though as it would make sense to allow different options here. Is this something you could create a pull request for?

randallknutson avatar Sep 25 '19 14:09 randallknutson

@randallknutson sure. ill check and see what all changes need to be done. is there a way I can communicate with you? I have couple of question.

Inaamhusain avatar Sep 26 '19 08:09 Inaamhusain

We track these issues pretty closely so feel free to cary on the questions here. We'll respond as soon as we're able.

randallknutson avatar Sep 26 '19 14:09 randallknutson

Just wants to ask these changes we need to do for builder as well as on Renderer side. Is that correct? If you can highlight little more about process from where I have to start with so that I can begin. And pull request should go to master if I am not wrong?

Inaamhusain avatar Sep 26 '19 18:09 Inaamhusain

If you are using the "url" provider there is no server side component for it. This assumes it is just uploading to the url provided and getting back a json response.

The way to go about this is to modify the data being sent to the url at https://github.com/formio/formio.js/blob/master/src/providers/storage/url.js#L86-L88

Add a configuration option at https://github.com/formio/formio.js/blob/master/src/components/file/editForm/File.edit.file.js that if you select "url" for your storage provider will give an option to modify the payload. This could either be to set the file key or mutate the request with javascript. I'd prefer the file key option as it is simpler and doesn't require running javascript. If that works for you, I'd go that direction.

Once you have the config, edit the url storage provider and use that setting to change the data payload.

randallknutson avatar Sep 27 '19 14:09 randallknutson

@randallknutson We have made necessary changes related to change a file in form-data to fileKey. Can you please validate below pull request. https://github.com/formio/formio.js/pull/1938

Inaamhusain avatar Oct 22 '19 14:10 Inaamhusain