form-data

FormData created from an existing form seems empty when I log it [duplicate]

天涯浪子 提交于 2019-11-27 20:01:55
This question already has an answer here: How to inspect FormData? 14 answers I'm trying to get a set of keys (input names or similar) and values (input values) from a web form: <body> <form> <input type="text" name="banana" value="swag"> </form> <script> var form = document.querySelector('form'); var formData = new FormData(form); </script> </body> According to the FormData documentation , formData should contain the keys and values from the form. But console.log(formData) shows formData is empty. How can I quickly get the data from the form using FormData? JSFiddle Farray Update : the XHR

fetch - Missing boundary in multipart/form-data POST

血红的双手。 提交于 2019-11-27 18:30:21
thanks for stopping by. I want to send a new FormData() as the body of a POST request using the fetch api the operation looks something like this var formData = new FormData() formData.append('myfile', file, 'someFileName.csv') fetch('https://api.myapp.com', { method: 'POST', headers: { "Content-Type": "multipart/form-data" }, body: formData } ) the problem here is that the boundary, something like boundary=----WebKitFormBoundaryyEmKNDsBKjB7QEqu never makes it into the Content-Type: header it should look like this Content-Type:multipart/form-data; boundary=---

Convert JS Object to form data

南笙酒味 提交于 2019-11-27 17:16:36
How can I can convert my JS Object to FormData ? The reason why I want to do this is, I have an object that I constructed out of the ~100 form field values. var item = { description: 'Some Item', price : '0.00', srate : '0.00', color : 'red', ... ... } Now I am asked to add the upload file functionality to my form which, of-course is impossible via JSON and so I am planning on moving to FormData . So is there any way that I can convert my JS object to FormData ? If you have an object, you can easily create a FormData object and append the names and values from that object to formData. You

Submitting a file with jQuery.ajax gives TypeError

泪湿孤枕 提交于 2019-11-27 12:59:51
问题 I am trying to submit a file from a form using jQuery's ajax method: var ofile=document.getElementById('image').files[0]; var formdata = new FormData(); formdata.append("image",ofile); $.ajax({ url:'elements/save_elements', data:formdata, type:'POST' }); This results in the error TypeError: 'append' called on an object that does not implement interface FormData . What causes this error? It doesn't happen on the actual formdata.append , but inside jQuery. 回答1: I was having the same problem

Uploading multiple files asynchronously by blueimp jquery-fileupload

我怕爱的太早我们不能终老 提交于 2019-11-27 09:26:24
I'm using jQuery File Upload library ( http://github.com/blueimp/jQuery-File-Upload ), and I've been stuck figuring out how to use the library satisfying the following conditions. The page has multiple file input fields surrounded by a form tag. Users can attach multiple files to each input field All files are sent to a server when the button is clicked, not when files are attached to the input fields. Upload is done asynchronously Say the page has 3 input fields with their name attributes being "file1[]", "file2[]" and "file3[]", the request payload should be like {file1: [ array of files on

AngularJS Formdata file array upload

二次信任 提交于 2019-11-27 08:10:54
问题 I'm trying to upload (actually POST) numerous small files in one go along with some key, value pairs: $scope.uploadFiles = function(files) { if (files.length === 0) { return; } var formData = new FormData(); formData.append('keyName1', 'keyValue1'); formData.append('keyName2', 'keyValue2'); formData.append('keyName3', 'keyValue3'); for (var i = 0; i < files.length; i++) { formData.append('files[]', files[i]); } $http.post( '/myEndpoint', formData, { headers: { 'Content-Type': undefined },

How to convert FormData(HTML5 Object) to JSON

*爱你&永不变心* 提交于 2019-11-27 06:48:00
How to convert HTML5 FormData object to JSON? Without Jquery and handling nested properties in FormData like a object. You could also use forEach on the FormData object directly: var object = {}; formData.forEach(function(value, key){ object[key] = value; }); var json = JSON.stringify(object); UPDATE: And for those who prefer the same solution with ES6 arrow functions : var object = {}; formData.forEach((value, key) => {object[key] = value}); var json = JSON.stringify(object); UPDATE 2: And for those who want support for multi select lists or other form elements with multiple values (since

Node.JS: How to send headers with form data using request module

会有一股神秘感。 提交于 2019-11-27 06:28:25
I have code like the following: var req = require('request'); req.post('someUrl', { form: { username: 'user', password: '', opaque: 'someValue', logintype: '1'}, }, function (e, r, body) { console.log(body); }); How can I set headers for this? I need user-agent, content-type and probably something else to be in the headers: headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36', 'Content-Type' : 'application/x-www-form-urlencoded' }; I've tried in multiple ways but I can either send header or form-data, failed

Retrofit 2 with only form-data

房东的猫 提交于 2019-11-27 05:23:11
问题 I am trying to make POST request using the Retrofit 2. The request type is form-data NOT application/x-www-form-urlencoded . I am only posting data not the files in the request and the response is in the form of JSON. I have tried @FormUrlEncoded, @Multipart but it is not working. I have tried following request 1. First Attempt @FormUrlEncoded @POST("XXXX") Call<PlanResponse> getPlanName(@Field(Constants.ACTION_ID) String actionId, @Field(Constants.OFFER_CODE) String offerCode); 2. Second

How to append multiple file inputs to a FormData object using $.each?

北慕城南 提交于 2019-11-27 02:51:27
问题 I have a multiple (and dynamic) number of inputs of type=file . I want to create a FormData object out of them. I need to manually append them to the object as I need access to their filenames for inserting into a database and therefore need to specify a filename is this format: myFormData.append(name,file,filename); HTML <form id="my_form" enctype="multipart/form-data"> <input type="file" name="undefined" id="file_1" data-filename="image.jpg"> <input type="file" name="undefined" id="file_2"