2016-02-19 55 views
0

我有一個接受丟棄文件(圖片)的組件,然後可以將它們上傳到服務器。HTTP在Angular2中發佈二進制文件

export class MyComp { 
    function save() { 
    let data : ArrayBuffer = this.readFile(this.file); 
    this.imageService.upload(data); 
    } 
} 

該服務將數據作爲二進制內容(內容類型是image/png)發佈到服務器。

@Injectable() 
export class ImageService { 
    private http: Http; 

    constructor(@Inject()http: Http) { 
     this.http = http; 
    } 
    upload(image: ArrayBuffer) { 
     let headers = new Headers({ 'Content-Type': 'image/png' }); 
     //let arr = new Int16Array(image); 
     //let body = String.fromCharCode.apply(null, arr); 
     return this.http 
      .put('/upload', body, { headers: headers }) 
      .map(response => response.json()); 
    } 

問題是我不能發送二進制數據(ArrayBuffer)。我嘗試發送ArrayBuffer(它發送一個字符串「ArrayBuffer」!),發送Int16Array(它發送更多字節),轉換爲字符串...但沒有任何作用。

回答

2

這些方法尚未實現;檢查來源requestresponse

export class Request { 
    // TODO: support URLSearchParams | FormData | Blob | ArrayBuffer 
} 
export class Response { 
    // TODO: Support ArrayBuffer, JSON, FormData, Blob 
}