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

1

1 Answer 1

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:

Sign up to request clarification or add additional context in comments.

2 Comments

tnx @darxtar but in my case, it might be image or doc or any kind of file .
edited my answer, your issue might be on the server side

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.