问题
Is it possible to send file with form as normal file upload? When I check a file field it is always empty, and I need to send it with form data in one call. IS that possible?
回答1:
There might be better answers out there, but I ended up solving this by following steps.
- Having 2 forms on the page. One for file upload and one for other form data.
- Format both forms using CSS so that user gets look and feel of one form.
- Have only one div (which looks like button) to submit. Hide the actual form's submit button, if you are using them (I don't. Because I preprocess data and then send by ajax)
- Once user submits forms (by clicking the dummy button), Using JS programatically click the file upload button.
- Optional Once that call is finished, if there are no errors only then continue.
- Submit the form with other form data.
The advantage I had in this approach was that I could have more control over user's form data as well as my file upload's settings. Because I had some settings (like max number of files and max file size) were dependent on user's form entry.
来源:https://stackoverflow.com/questions/24386082/blue-imps-jquery-file-upload-send-files-with-form-like-normal-field