MoOx / pjax

Easily enable fast Ajax navigation on any website (using pushState + xhr)
MIT License
1.45k stars 124 forks source link

Question: Form with "multipart/form-data" #132

Closed DmitryNiko closed 6 years ago

DmitryNiko commented 6 years ago

Hi! Do you have plans to add support for submitting the form using the encryption "multipart/form-data"? This would allow uploading files using pjax, which would be great.

BehindTheMath commented 6 years ago

Why wouldn't you use a regular AJAX call for that? Are you expecting an HTML response?

DmitryNiko commented 6 years ago

I previously used the jquery-form library in my laravel application for multiple files uploading. Now, having switched to pjax, in forms I use the default html display of validation messages in my views. It would be great to use files uploads in pjax without extra libraries.

BehindTheMath commented 6 years ago

@DmitryNiko Can you explain a bit more what you're trying to do? Are you trying to upload files to an endpoint that returns an HTML message?

DmitryNiko commented 6 years ago

@BehindTheMath Yes. I use the simple form submiting, which returns html response, and if pjax will supports submiting form with multipart encryption via js FormData(), there would be no need to use another method for submiting form with multipart.

BehindTheMath commented 6 years ago

Can you post the HTML and JS code you're using to upload the form? You can use something like CodePen if that's easier.

BehindTheMath commented 6 years ago

@robinnorth @zeraphie I just took another look at this after working with #146 and #147, and I think it would be a good idea to allow the user to override the default encoding by setting the enctype attribute on the form.

So Pjax would first check the enctype attribute, and if it wasn't set, use the default application/x-www-form-urlencoded encoding instead.

This would avoid any breaking changes, but allow the user to choose which encoding they want to use.

zeraphie commented 6 years ago

That's interesting, would it still use the parser that's currently being used? Or would you have an add on method for parsing the fields of the form?

BehindTheMath commented 6 years ago

If the user specified the encoding as multipart/form-data, it would just use FormData, and not have to parse it at all, like you wanted originally.

robinnorth commented 6 years ago

I can see the benefits of supporting both types of form encoding, so long as it doesn't make the library unnecessarily bloated or complex to test and maintain. If there's a danger of that happening, perhaps we should just switch to FormData in the next major version (and drop IE9 support?). Anyone who specifically requires application/x-www-form-urlencoded support can continue to use the last version released before this change.

BehindTheMath commented 6 years ago

@robinnorth I put together a PR to add this feature, and it doesn't seem to be complex.

It's dependent on #148, so I'm going to wait to submit it until that is merged.

robinnorth commented 6 years ago

Great, I only had a couple of comments to resolve on #148 before that can be merged. Looking forward to seeing the PR for this!