Skip to content
Advertisement

Using jQuery val() to send form data and using FormData to send for data

I tried to send a file using an Ajax POST request using two methods:

Method 1 (jQuery val())

JavaScript

Method 2- (FormData)

JavaScript

Now, Method 2 worked, but Method 1 did not. What is the reason for that?

Advertisement

Answer

$('#fileinputid').val() only gets you the file name. You can not upload a file with that.

FormData is capable of creating the whole multipart/formdata request structure that is needed for a file upload.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement