I have used ember-plupload for uploading the images.Now I have written a component for this.I am able to open and select the images from my machine.But I am lost what to do next.I have to send the post request to my server endpoint with body params { photo_file: image file }.My code is following.
component.hbs
{{#pl-uploader extensions="jpg jpeg png gif" for="upload-image" onfileadd="uploadImage" as |queue features|}}
<div class="dropzone" id={{dropzone.id}}>
<a id="upload-image">Add an Image.</a>
</div>
{{/pl-uploader}}
component.js
actions:{
uploadImage:function(file){
console.log(file)
let filename = file.get('name');
file.read().then(function (url) {
console.log(filename)
console.log(url)
}
}
I am able to get the file name and encode base64 value.But not sure how to send the request to server endpoint.
http://example.com/api/addphoto and it require body with parameter photo_file and choosed file.
I am able to make the correct request from postman app.In the body of the request ,I am selecting the file option and it directly gives me the option to choose a file from there itself.The request is made successfully and photo gets added to endpoint when I select an image and send request
How should I do it in my app?
You could use an XMLHttpRequest
uploadImage:function(files){
var file = files[0];
var request = new XMLHttpRequest();
request.open('post', "target url", true);
var formData = new FormData();
var fieldName = 'file';
formData.append(fieldName, file);
request.onreadystatechange = Ember.run.bind(this, function () {
if (request.readyState === 4 && request.status !== 0) {
//success
}
});
request.send(formData);
}