Aurelia js fie upload to server

2019-03-06 21:14发布

问题:

Hi am new to aurelia js , i need to upload file to server,am using autrelia js, materializecss and httpClient.fetch for api call. I dont'know how to send file to server.

view :

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

Model :

  onSelectFile(e)
{
   var myurl = 'http://cdn.dmsapp.tk/file?authToken=bLNYMtfbHntfloXBuGlSPueilaHtZx&type=jpg&name=sibi.jpg&userId=7&organizationId=1&sourceType=USER_UPLOADS';
        this.httpValueConverter.call_http(myurl,'POST',this.selectedFiles[],'fileupload',file_upload)
            .then(data => {
            console.log(data);
        if(data.meta && data.meta.statusCode == 200) {
          //  this.index_lists = data.index.list;
        }
    }); }

httpservice :

 return this.httpClient.fetch('http://api.dmsapp.tk/'+url,
            {
                method: method,
                body : json(myPostData),
                headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'authorization': this.authorization}})
            .then(response => response.json());

looking for a solution.

回答1:

If it's a file and you are trying to upload a particular media type, the header 'Content-Type': 'application/x-www-form-urlencoded' does not seem right to me. Have a look at the appropriate media type here:

http://www.iana.org/assignments/media-types/media-types.xhtml

Also, you serialize data to JSON, if your data is binary you will need to change that to a byte array.

You might find some useful info here:

http://www.petermorlion.com/file-upload-with-aurelia/

Also you set a token both in your URL and your header, I'd recommend to set it in the header only.