从Angular上传文件 – 将文件内容设置为DTO

huangapple go评论62阅读模式
英文:

File upload from Angular - Set file content to Dto

问题

我在Swagger中添加了一个IOperationFilter,以便我可以从Swagger UI测试我的API的文件上传。我的DTO如下:

public class ResourceCreateDto : EntityDto
{
    public string Title { get; set; }
    public string Description { get; set; }
    public IFormFile File { get; set; } // 文件内容在这里
    public string ExtendedData { get; set; }
}

ASP.NET Core API如下:

public async Task<ResourceDto> Create([FromForm] ResourceCreateDto input)

这样,我可以同时上传文件和其他数据(有些人建议先保存文件,然后在另一个调用中保存数据)。

我不确定的是如何在Angular端与Abp一起使用它。尽管代理已生成,但如何让Angular客户端使用[File]属性并将其数据与其余部分一起发送到服务器API呢?

英文:

I added a IOperationFilter to swagger so that I can test file uploads for my apis from swagger ui. My Dto is like;

public class ResourceCreateDto : EntityDto
{

    public string Title { get; set; }

    public string Description { get; set; }

    public IFormFile File { get; set; } //file content here

    public string ExtendedData { get; set; }
}

Asp.net Core API is:

public async Task&lt;ResourceDto&gt; Create([FromForm] ResourceCreateDto input)

This way I can upload file & other data at the same time (Some have suggested to save file 1st and then save data in another call).

What I am not sure is how to use it in Angular end with Abp. Even though proxies are generated how can I get the angular client to use [File] property and post it's data with the rest to server api?

答案1

得分: 1

create DTO:

export class ResourceCreateDto {
    title: String;
    description: String;
    extendedData: String;
    file: File;
}

in HTML:

<input class="form-control" type="text" [(ngModel)]="resourceCreateDto.title"/>
<input class="form-control" type="text" [(ngModel)]="resourceCreateDto.description"/>
<input class="form-control" type="file" [(ngModel)]="resourceCreateDto.extendedData"/>
<input class="form-control" type="text" [(ngModel)]="resourceCreateDto.file" (change)="onChangeFile($event)"/>
<button class="btn btn-primary" type="button" (click)="submit()">Add new</button>

in component:

resourceCreateDto: ResourceCreateDto;
file: any;

ngOnInit(): void {
    //...
    this.resourceCreateDto = new ResourceCreateDto();
}

onChangeFile(event) {
    this.file = event.srcElement.files;
}

submit() {
    let formData: FormData = new FormData();
    formData.append('title', this.resourceCreateDto.title + '');
    formData.append('description', this.resourceCreateDto.description + '');
    formData.append('extendedData', this.resourceCreateDto.extendedData + '');
    formData.append('file', this.file[0], this.file[0].name);
    
    // use service to call API
    // declare your httpClient
    this.httpClient.post<httpResponse>(`${this.BASEURL}/...`, formData, this.getHttpFileOptions(your token))
    .subscribe(response => {
        console.log(response);            
    },
    error => {
        console.log(error);
    });
}

setting option for HTTP:

getHttpFileOptions(token: String): object {
    return {
        observe: 'response',
        responseType: 'json',
        headers: new HttpHeaders({
            "Accept": "application/json",
            'Authorization': 'Bearer ' + token
        })
    };
}

action in server:

[HttpPost("Create")]
public IActionResult Create([FromForm] ResourceCreateDto model, IFormFile File)
英文:

create DTO:

export class ResourceCreateDto{
	title : String;
    description: String;
    extendedData : String;
    file: File;
}

in html

&lt;input class=&quot;form-control&quot; type=&quot;text&quot; [(ngModel)]=&quot;resourceCreateDto.title&quot;/&gt;
&lt;input class=&quot;form-control&quot; type=&quot;text&quot; [(ngModel)]=&quot;resourceCreateDto.description&quot;/&gt;
&lt;input class=&quot;form-control&quot; type=&quot;file&quot; [(ngModel)]=&quot;resourceCreateDto.extendedData&quot;/&gt;
&lt;input class=&quot;form-control&quot; type=&quot;text&quot; [(ngModel)]=&quot;resourceCreateDto.file&quot; (change)=&quot;onChangeFile($event)&quot;/&gt;
&lt;button class=&quot;btn btn-primary&quot; type=&quot;button&quot; (click)=&quot;submit()&quot;&gt; Add new &lt;/button&gt;

in component

    resourceCreateDto: ResourceCreateDto;
	file: any;
	ngOnInit(): void {
		//..
        resourceCreateDto = new ResourceCreateDto();
    }
    onChangeFile(event){
		this.file = event.srcElement.files;
	}
    submit(){
		let formData: FormData = new FormData();
		formData.append(&#39;title&#39;, this.resourceCreateDto.title  + &#39;&#39;);
		formData.append(&#39;description&#39;, this.resourceCreateDto.description + &#39;&#39;);
		formData.append(&#39;extendedData&#39;, this.resourceCreateDto.extendedData  + &#39;&#39;);
		formData.append(&#39;file&#39;, this.file[0], this.file[0].name);
		// use service call api
        // declare your httpClient
        this.httpClient.post&lt;httpResponse&gt;(`${this.BASEURL}/...`, formData, 
        this.getHttpFileOptions(your token))
		.subscribe(response =&gt;{
			console.log(response);			
		},
		error=&gt;{
			console.log(error);
		})
	}

setting option http

getHttpFileOptions(token: String): object{
    return {
	    observe: &#39;response&#39;,
		responseType: &#39;json&#39;,
		headers: new HttpHeaders({
			&quot;Accept&quot;: &quot;application/json&quot;,
			&#39;Authorization&#39;: &#39;Bearer &#39; + token
		})
	};
}

action in server

   [HttpPost(&quot;Create&quot;)]
   public IActionResult Create([FromForm]ResourceCreateDto model, IFormFile File)

Hope to help you!

huangapple
  • 本文由 发表于 2020年1月3日 23:16:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581023.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定