0

I need to upload files in Aurelia Js. I don't know how to send file to server(hit to given API). I have two doubts, one is in my 'content-type' and another one is, do i need to change my image object to any other format. Here is the code i used,

scanupload.html :

<input type="file"  files.bind="selectedFiles" change.delegate="onSelectFile($event)">

scanupload.js :

 onSelectFile($event){ var myurl = 'http://cdn.dmiapp.tk/file?authToken=bLNYMtfbHntfloXBuGlSzPueilaHtZx&type=jpg&name=testfile.jpg&organizationId=1&userId=7&sourceType=USER_UPLOADS';

    this.httpValueConverter.call_http(myurl,'POST',{file :this.selectedFiles[0]},'fileupload')
        .then(data => {
        console.log(data);
    if(data.meta && data.meta.statusCode == 200) {
        console.log('success');
    }
});}

httpservice.js :

 call_http(url,method,myPostData,action,params) {
    return this.httpClient.fetch(url,
                {
                    method: method,
                    body : myPostData,
                    headers : {
                        'authorization': this.authorization,
                        'Content-Type':'form-data'
                    }
                })
                .then(response => response.json());}

Error : bad request and unsupported media file. Also tried content type, form-data and multipart/form-data

sibi
  • 635
  • 3
  • 8
  • 26
  • Possible duplicate of [Posting data and file with Aurelia to ASP.NET webapi](http://stackoverflow.com/questions/37589636/posting-data-and-file-with-aurelia-to-asp-net-webapi) – Matthew James Davis Oct 19 '16 at 08:17

1 Answers1

0

If it's an image your media type must be: "image/jpeg" and you can try to upload the image as a blob.

This does not seem to be an Aurelia-specific issue.

Try first to make a simple example working such as:

Upload image using javascript

When this works refactor it to an Aurelia view.

Also, verify that you have indeed configured your server to support the necessary mime type.

For multipart/form-data you can look at the following tweaks and packages:

Community
  • 1
  • 1
Darxtar
  • 2,022
  • 22
  • 21