Upload a file with from vuejs app using ajax post

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Upload a file with from vuejs app using ajax post



Does anyone know how to upload a file in a vuejs app and pass it to a php page using jquery's ajax function. Please for the love of all thats holy I need some help with this. I essentially want to do this but run it in a vuejs method. (the following script works outside of the method and the post and files array is set. but when placed in a method it no longer works. In other words the
$_FILES and $_POST array is not set) I would prefer to do this without axios or any other external libery other than jquery if possible. Does anyone know if you can even do this in vuejs?


$(document).ready(function()
$('#addTemplateForm').on('submit', function(e)
e.preventDefault();
app.sub=true;
if(app.name!='' && app.thumbnailName!='' && app.renderTime!=''
&& app.textFieldCount!='' && app.selectedCategories.length!=0 && app.selectedKeywords.length!=0)
$.ajax(
url:'addTemplateBackend.php',
type:'POST',
dataType: 'json',
data: new FormData(this),
contentType: false,
processData: false,
error: function(data)
alert('error');
,
success: function(data)
alert('success');
console.log(data);

)
);
);




1 Answer
1



Please describe the error you are having. You can check your console and also use the Vue tool plugin extension for browsers so as to help you with trouble shooting.



PS: This was a mistake. Meant to be a comment not an answer





Please describe the error you are having. You can check your console and also use the Vue tool plugin extension for browsers so as to help you with trouble shooting.
– Ezugudor
Aug 9 at 19:25





refresh the page guys I hope thats a little clearer. I want to upload a file and pass form data to a php page from vuejs with an ajax call to a php page. So I can insert form data into a database as well as upload the file. However when I run that function in a vue method. It does not set $_POST and $_FILES array
– codernoob6
Aug 9 at 19:26





You have to put this code snippet within/inside the Vuejs MOUNTED hook(method) . And you dont need the jquery document.ready() since the Vue js mounted hook takes care of this
– Ezugudor
Aug 9 at 19:37







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard