Hỏi đáp

Chia sẻ kiến thức, cùng nhau phát triển

Upload file image với angular 5

7 năm trước 1.967 lượt xem 4 bình luận 7 năm trước

 createImages(event: any,productId:string,token:string) {

        let fileList: FileList = event.target.files;
        if (fileList.length > 0) {
            let file: File = fileList[0];
            let formData: FormData = new FormData();
            formData.append('file', file, file.name);

            return this.http.post(this.Url + 'api/product/' + productId + '/Images', formData,
                    ({
                        headers: {
                            //USE credentials mode
                            Accept: 'application/json',
                            'Content-Type':'multipart/form-data',
                            withCredentials: true,
                            'Authorization': 'Bearer ' + token
                        }
                    }) as any)
                .map(res => res.json()).catch(error => Observable.throw(error))
                .subscribe(
                    data => console.log('success'),
                    error => console.log(error)
                );

        }

    }

 

html 

     <input type="file" id="exampleInput" (change)="createImages($event,itemProduct.id,token)" style="display: none" accept=".png,.jpg"/>

Mình muốn upload 1 file hình ảnh lên server  sử dụng api (asp.net core) Mình đã config như thế nhưng nó không hoạt động.(API đã test trên postman và chạy ngon lành). mọi người   giúp mình config  với ạ. cảm ơn ạ

 

Bình luận

Để bình luận, bạn cần đăng nhập bằng tài khoản Howkteam.

Đăng nhập
toilahoi007 đã bình luận 7 năm trước

Em cảm ơn ạ.. em sửa được rồi .. bỏ 'Content-Type':'multipart/form-data', đi là được

tran.thuan.nghia đã bình luận 7 năm trước

- Front-end của bạn có gọi được những API khác của .NET Core ko?
- Có thông báo lỗi gì khi upload image ko?

Bài viết
0
Câu hỏi
3
Bình luận
3