相关疑难解决方法(0)

如何在Angular 2中发布FormData对象?

我需要上传一个文件并发送一些json,我有这个功能:

POST_formData(url, data) {
        var headers = new Headers(), authtoken = localStorage.getItem('authtoken');

        if (authtoken) {
            headers.append("Authorization", 'Token ' + authtoken)
        }

        headers.append("Accept", 'application/json');
        headers.delete("Content-Type");

        var requestoptions = new RequestOptions({
            method: RequestMethod.Post,
            url: this.apiURL + url,
            headers: headers,
            body: data
        })

        return this.http.request(new Request(requestoptions))

        .map((res: Response) => {
            if (res) {
                return { status: res.status, json: res.json() }
            }
        })
    }
Run Code Online (Sandbox Code Playgroud)

我的问题是,如果我设置content-type为" multipart/form-data"我的服务器抱怨边界,如果我content-type完全删除标头,我的服务器抱怨它" text/plain"支持的媒体类型.

那么,如何使用angular2发送FormData?

file-upload typescript angular2-http angular

42
推荐指数
3
解决办法
9万
查看次数

如何在Angular2中上传文件

我必须提交一份表格和图片.我试过这个代码(有多种方式),但对我来说不起作用.有没有人有使用angular2上传文件的工作演示请帮助我.

component.html

    <form class="form-horizontal" role="form" >

        <div class="form-group">
            <label class="control-label col-sm-4" for="myname" style="">Name<span class="asterisk">*</span></label>
            <div class="col-sm-7">
                <div>
                    <input type="text" class="form-control" id="myname"
                    [(ngModel)]="myfile.name">                        
                </div>                  
            </div>                               
        </div>        


        <div class="form-group">
            <label class="control-label col-sm-4" for="myimage">Image</label>
            <div class="col-sm-7">
                <div>
                    <input type="file" (change)="fileChangeEvent($event)" placeholder="Upload file..." />                         
                </div>   
            </div>
        </div>


        <div class="form-group">        
        <div class="text-center">
            <button type="button" (click)="upload()">Upload</button>             
        </div>
        </div>
  </form>
Run Code Online (Sandbox Code Playgroud)

component.ts

     myfile={
                "name":"Mubashshir",               
                "image":''
     }

     fileChangeEvent(fileInput: any){
        this.myfile.image = fileInput.target.files;        
     }

     upload(){
          this.base_path_service.PostRequest('http://128.199.190.109/api/school/schoolDetail/',this.myfile)
            .subscribe(
                data => {
                            console.log("data submitted");                        
                        },
                err => console.log(err),
                () =>{
                     console.log('Authentication Complete'); …
Run Code Online (Sandbox Code Playgroud)

file-upload angular2-forms angular

40
推荐指数
2
解决办法
9万
查看次数

如何angular2发布JSON数据和文件在同一请求中

我想在同一个请求中实现post文件和Json数据.

以下是上传文件代码:

upload(url:string,file:File):Observable<{complate:number,progress?:number,data?:Object}>{


    return Observable.create(observer => {
      const formData:FormData = new FormData(),
        xhr:XMLHttpRequest = new XMLHttpRequest();
      formData.append('uploadfile', file);


      formData.append("_csrf", this.tokenService.getCsrf());
      xhr.open('POST',url, true);
      xhr.onreadystatechange = () => {
        if (xhr.readyState === 4) {
          if (xhr.status === 200) {
            observer.next({complate:1,progress:100,data:JSON.parse(xhr.response)});
            observer.complete();
          } else {
            observer.error(xhr.response);
          }
        }
      };

      xhr.upload.onprogress = (event) => {
        observer.next({complate:0,progress:Math.round(event.loaded / event.total * 100)});
      };


      const headers=new Headers();
      let token: string = localStorage.getItem('access-token');
      xhr.setRequestHeader('Authorization', `Bearer ${token}`);
      xhr.send(formData);
    }).share();
Run Code Online (Sandbox Code Playgroud)

如何与angular2 http.post(url,JSON.stringify(data))集成.

file-upload typescript angular

12
推荐指数
3
解决办法
6426
查看次数

ElementRef安全风险角度2

为什么ElementRef不安全,如果是这样,我们可以使用什么呢?

我一直在使用这个ElementRef来查看或观看特定的html标签,然后在初始化之后作为特定宽度发送,但是如果这打开了安全风险我将不会使用它,并且说实话我不明白为什么角度2个团队在他们的框架中允许这种安全漏洞.

什么是安全和最好的技术?我的测试组件如下:

        import { Component, OnInit } from '@angular/core';

        @Component({
          selector: 'app-standard',
          template: ` <button type="button" #buttonW></button>`,

        })
        export class standardComponent implements OnInit {

          name: string = 'app-standard';
          viewWidthButton: any;

          @ViewChild( 'buttonW' ) elButtonW: ElementRef; 


          constructor() { 

            this.viewWidthButton = this.elButtonW.nativeElement.offsetWidth;
            console.log ('button width: ' + this.viewWidthButton);
          }

          ngOnInit() {
          }

        }
Run Code Online (Sandbox Code Playgroud)

Angular 2页面参考:

https://angular.io/docs/ts/latest/api/core/index/ElementRef-class.html

angular

6
推荐指数
1
解决办法
1829
查看次数