[英]Upload File from Angular to .Net
我正在嘗試使用我的表單和文件發布帖子。 我嘗試查看其他帖子但沒有結果。 如果我在沒有文件的情況下發布帖子,就可以了。 英語不是我的母語,如有錯誤請見諒。
這是錯誤:
這是我的數據:
我的代碼-> .Net
模型:
public class EmailDto
{
public int Id { get; set; }
public string Name { get; set; }
public string Subject { get; set; }
public int? RecurrentDay { get; set; }
public DateTime? SendDate { get; set; }
public short SendHour { get; set; }
public int? GroupId { get; set; }
public int? RecipientTypeId { get; set; }
public int? ListTypeId { get; set; }
public bool SendForce { get; set; }
public string? Recipients { get; set; }
public bool Archived { get; set; }
public bool Active { get; set; }
public int IdTemplate { get; set; }
public TemplateDto Template { get; set; }
public int? IdRecurrence { get; set; }
public IFormFile? File { get; set; }
}
控制器:
[HttpPost("create")]
public async Task<ActionResult> CreateEmail([FromBody] EmailDto email)
角->
模型:
export class EmailModel {
id!: number
idTemplate!: number
idRecurrence!: number
name!: string
subject!: string
active!: boolean
groupId!: number
recipientTypeId!: number
listTypeId! : number
recipients!: string
sendHour!: number
sendForce!: boolean
template!: TemplateModel
file!: File
}
電子郵件.component.ts:
onFileSelected(event: any) {
let fileList: FileList = event.target.files;
if (fileList.length > 0) {
this.file = fileList[0];
}
}
async onSubmit() {
if (this.file != null) {
this.email.file = this.file;
let headers = new HttpHeaders();
headers.append('Content-Type', 'multipart/form-data');
headers.append('Accept', 'application/json');
this.createEmail(this.email, headers);
} else {
this.createEmail(this.email);
}
}
createEmail(email: EmailModel, headers?: HttpHeaders) {
if (headers != null) {
this.EmailService.CreateEmail(email, headers).subscribe(() => {
this.isCreated = true;
}), (error: any) => {
console.log(error);
}
} else {
this.EmailService.CreateEmail(email).subscribe(() => {
this.isCreated = true;
}), (error: any) => {
console.log(error);
}
}
}
服務:
CreateEmail(data: EmailModel, headers?: HttpHeaders) {
return this.http.post(`${this.emailApi}/create`, data, {headers: headers});
}
最后打印:
如果我正確理解了您的問題,您似乎正在嘗試發送帶有文件的 JSON 對象主體,並且在 .NET 端,它無法反序列化它。
您可以嘗試將文件作為 base64 發送,而不是像現在一樣嘗試發送:
async onSubmit()
{
if (this.file == null)
{
this.createEmail(this.email);
return;
}
// Create a file reader
const fileReader = new FileReader();
// Tell the file reader what to do once it loads
fileReader.onload = (event) =>
{
// which is to give us the base64 representation of the file
const base64File = event.target.result.toString();
// and assign it to the dto
this.email.file = base64File;
// before sending the request.
this.createEmail(this.email);
};
// Finally, read the file.
fileReader.readAsBinaryString(this.file);
}
當然,您需要將file
更改為 DTO 中的字符串。 然后,您可以將 base64 解析回二進制文件並使用它做任何您想做的事情。 如果您需要 IFormFile,則:
[HttpPost("create")]
public async Task<ActionResult> CreateEmail([FromBody] EmailDto email)
{
var byteArray = Convert.FromBase64String(email.File);
IFormFile file;
using(var memoryStream = new MemoryStream(byteArray))
{
file = new FormFile(memoryStream,
0,
byteArray.Length,
"someName",
"someFileName");
}
...
doWhatever
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.