Wir implementieren Kendo für Angular in unserem Projekt. Wir haben ein bestehendes Projekt, das Kendo-Upload verwendet, das sofort einen Aufruf an den Server auslöst, aber das kann ich für diese Seite nicht tun.
Die Seite ist für einen Mitarbeiter zum Hochladen eines Lebenslaufs. Sie haben möglicherweise einen vorhandenen Lebenslauf und müssen zuerst gefragt werden, ob sie diesen ersetzen möchten, und sie erhalten ein Feld zur Eingabe einer Beschreibung. Daher denke ich, dass ich kendo-fileselect verwenden und möglicherweise einen Dialog auslösen muss (sind Sie sicher, dass Sie ersetzen möchten?) und die Beschreibung und die Mitarbeiter-ID auf Knopfdruck mitsenden. Also habe ich ein Objekt erstellt, um diese Daten zu tragen.
Sie können sehen, dass das Objekt aufgefüllt wird, wenn es die API aufruft:
Aber wenn ich versuche, die API-Methode aufzurufen, erhalte ich eine Fehlermeldung:
Failed to load resource: the server responded with a status of 400 (Bad Request) [http://localhost:6300/api/employee/resume]
Der entsprechende Code ist unten.
Serverseitig:
public class EmployeeResume
{
public int ResumeId { get; set; }
public int EmployeeId { get; set; }
public DateTime CreatedDate { get; set; }
public string Description { get; set; }
public byte[] FileContent { get; set; }
public string FileName { get; set; }
}
[HttpPut("resume")]
public async Task<IActionResult> UploadResume([FromBody] EmployeeResume resume)
{
if (resume == null)
{
return BadRequest();
}
return Ok();
}
Client-Seite:
// Model
export interface EmployeeResume {
createdDate?: string;
description?: string;
employeeId: string;
fileContent: any;
fileName: string;
resumeId?: string;
}
// CHILD component
// -----------------------------------------------------------
// Kendo FileSelect in template
<kendo-fileselect
formControlName="uploadFile"
[restrictions]="uploadRestrictions"
[multiple]="false"
(select)="selectEventHandler($event)">
</kendo-fileselect>
// Sets a component property to the selected file
selectEventHandler(e: SelectEvent): void {
this.uploadfile = e.files[0];
}
// When upload button is clicked, create the
// resume object and emit an event to parent
upload(): void {
if (this.uploadfile.validationErrors) return;
const thisComponent = this;
const reader = new FileReader();
reader.onload = function (ev) {
const request = {
description: thisComponent.f.description.value,
employeeId: thisComponent.employeeId,
fileContent: ev.target.result,
fileName: thisComponent.uploadfile.name
} as EmployeeResume;
thisComponent.onUpload.emit(request);
}
reader.readAsDataURL(this.uploadfile.rawFile);
}
// PARENT component
// -----------------------------------------------------------
// Template
<app-employee-resume
[employeeId]="(employeeId$ | async)"
(onUpload)="uploadResume($event)">
</app-employee-resume>
// Handler
uploadResume(resume: EmployeeResume) {
this.svc.upsertResume(resume)
}
// 'svc'
upsertResume(resume: EmployeeResume) {
return this.http.put(`${this.apiUrl}/employee/resume`, resume);
}
Sie können dafür das ts FormData und ein benutzerdefiniertes FileData-Objekt mit beliebigen zusätzlichen Eigenschaften verwenden, die Sie in der DB speichern möchten:
onFileSelected(e: SelectEvent): void {
this.uploadfile = e.files[0].rawFile;
}
upload(): void {
const formData = new FormData();
const fileForSave = new FileData();
fileForSave.fileCategoryId = FileCategory.EmployeeResume;
fileForSave.description = this.f['description'].value;
fileForSave.employeeId = this.employeeId;
if (this.uploadfile) formData.append('file', this.uploadfile);
formData.append('fileForSave', JSON.stringify(fileForSave));
this.onUpload.emit(formData);
this.resetForm();
}
addFile(form: FormData): Observable<FileData> {
return this.http.post<FileData>(`${this.apiUrl}/file/`, form);
}
Dann sieht Ihre API-Methode etwa so aus:
[HttpPost]
public async Task<IActionResult> AddFile(
IFormFile file,
[ModelBinder(BinderType = typeof(JsonModelBinder))] Contracts.Entities.File fileForSave)
{
// Check any file validations if you want
/* Save the IFormFile file to disk */
/* Save entity fileForSave File object to the DB */
return Ok(newFile);
}
Und ich werde das sogar für dich einwerfen:
public class JsonModelBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null)
{
throw new ArgumentNullException(nameof(bindingContext));
}
// Check the value sent in
var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (valueProviderResult != ValueProviderResult.None)
{
bindingContext.ModelState.SetModelValue(bindingContext.ModelName, valueProviderResult);
// Attempt to convert the input value
var valueAsString = valueProviderResult.FirstValue;
var result = Newtonsoft.Json.JsonConvert.DeserializeObject(valueAsString, bindingContext.ModelType);
if (result != null)
{
bindingContext.Result = ModelBindingResult.Success(result);
return Task.CompletedTask;
}
}
return Task.CompletedTask;
}
}
Dieser Artikel stammt aus dem Internet. Bitte geben Sie beim Nachdruck die Quelle an.
Bei Verstößen wenden Sie sich bitte [email protected] Löschen.
Lass mich ein paar Worte sagen