CODIGO INPUT TYPE IMG formData.append POST FETCH JS
const title = document.getElementById('title').value;
const author = document.getElementById('author').value;
const isbn = document.getElementById('isbn').value;
const image = document.getElementById('image').files;
const formData = new FormData();
formData.append('image', image[0])
formData.append('title', title)
formData.append('author', author)
formData.append('isbn', isbn)
////////////////////
e.preventDefault();
const data = new FormData();
const inputfile = document.getElementById('inputfile');
data.append('id', this.props.user.id);
data.append('avatar', inputfile.files[0]);
axios.post('/api/avatar', data, {
headers: {
'Content-Type': 'multipart/form-data',
},
})
.then(() => {
socketEmit.getAvatar();
this.props.onRequestClose();
})
.catch(() => {
this.setState({ error: 'Only .jpg, .png or .gif, < 5MB' });
});
}
//////////////////////
.getElementById('submit-button')
.addEventListener('click', event => {
// prevent the default form submission behavior
event.preventDefault();
// create a FormData object to hold the form values
const formData = new FormData();
// get the text from the text input
formData.append('testText', textInput.value);
// get the file from the file input
formData.append('testFile', fileInput.files[0]);
// send the data to the server
fetch(
'/upload',
{
method: 'POST',
body: formData
}
)
.then(() => {
// update the file list after uploading
listUploadedFiles();
});
});
//////////////////////////////////////
e.preventDefault();
const data = new FormData();
const inputfile = document.getElementById('inputfile');
data.append('id', this.props.user.id);
data.append('avatar', inputfile.files[0]);
axios.post('/api/avatar', data, {
headers: {
'Content-Type': 'multipart/form-data',
},
})
.then(() => {
socketEmit.getAvatar();
this.props.onRequestClose();
})
.catch(() => {
this.setState({ error: 'Only .jpg, .png or .gif, < 5MB' });
});
}
////////////////////////////////////////////////
const file = document.getElementById("fileupload"); //the File Upload input
const formdata = new FormData();
formdata.append("userpic", file.files[0],'Test1.jpg');
fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: formdata
})
//////////////////////////////////////////////////
function getFiles(){
var files = document.getElementById("myFiles").files;
var myArray = [];
var file = {};
console.log(files); // see the FileList
// manually create a new file obj for each File in the FileList
for(var i = 0; i < files.length; i++){
file = {
'lastMod' : files[i].lastModified,
'lastModDate': files[i].lastModifiedDate,
'name' : files[i].name,
'size' : files[i].size,
'type' : files[i].type,
}
//add the file obj to your array
myArray.push(file)
}
//stringify array
console.log(JSON.stringify(myArray));
}
///////////////////////
var formData = new FormData();
var file = document.querySelector('#file');
formData.append("file", file.files[0]);
formData.append("document", documentJson);
axios({
method: 'post',
url: 'http://192.168.1.69:8080/api/files',
data: formData,
})
.then(function (response) {
console.log(response);
})
.catch(function (response) {
console.log(response);
});
Comentarios
Publicar un comentario