Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to do with vform post with header multipart/form-data #54

Closed
shiroamada opened this issue Jan 24, 2019 · 6 comments
Closed

how to do with vform post with header multipart/form-data #54

shiroamada opened this issue Jan 24, 2019 · 6 comments

Comments

@shiroamada
Copy link

how to do with vform post with header multipart/form-data?
I saw your example is when select input file it do ajax file upload.
But for my case, I need to submit together with other form data. I am upload an excel file
Is there any way In your this.form.post(), to add the header info?

@cretueusebiu
Copy link
Owner

With this example you can upload files and still pass along other fields.

@shiroamada
Copy link
Author

Do you mind show me the example? Ya, I read your example upload, how do you send header?

{headers: {
        'Content-Type': 'multipart/form-data'
}}

          this.form.submit('post', '/upload', {
              // Transform form data to FormData
              transformRequest: [function (data, headers) {
                return objectToFormData(data)
              }],

@shiroamada
Copy link
Author

Ok, I found what out what is the issue.
I didn't notice, you imported extra library call 'objectToFormData.js', that why I can't convert my file object. When I post to the URL. It keep showing empty.

My first thought is the header problem, and end up it is the key point objectToFormData. Once I imported to my script it work like charm.

Thanks!

For those who want to change the headers would be like below. But for case, the library will change according. If you change manually it will break the code.

this.form.submit('post', '/upload', {
              //change custom header, for my case, it does not need it, if you add it. You break the code.
              headers: { 'content-type': 'multipart/form-data' },
              // Transform form data to FormData
              transformRequest: [function (data, headers) {
                return objectToFormData(data)
              }],

@mohdabbas
Copy link

mohdabbas commented Feb 13, 2019

Is this working with this.form.submit('put'....? Working perfectly with post, but not with Put! Sends null towards PHP Controller!

@cretueusebiu
Copy link
Owner

This is a bug in PHP where you can't send files via the PUT method.
With Laravel what you can do is set the method to POST add a field _method = PUT and will work.
laravel/framework#13457

@mohdabbas
Copy link

I added the field, but it doesn't work. Can you please share an example?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants