У меня есть монолитный проект JHipster (v5.6.1), и мне нужно реализовать вложения файлов в сущность.
Я не знаю, с чего начать. DTO отправляются на контроллеры REST для их создания или обновления. Как мне отправить файл?
Я мог бы присоединить Base64 к своему DTO и отправить его таким образом, но я не уверен, как (есть ли угловые плагины для этого?). Это потребует дополнительной работы для сервера и, как мне кажется, дополнительного размера файла.
Другой вариант - отправить объект (DTO) и файл отдельно, опять же, я не совсем уверен, как.
Большое спасибо, пожалуйста, простите за плохое форматирование. Я не очень сообразителен с SO:)
UPDATE
Вот как это выглядит в настоящее время (ваниль Jhipster 5):
образец-update.component.html
<form name="editForm" role="form" novalidate (ngSubmit)="save()" #editForm="ngForm">
<!-- DTO fields -->
<div class="form-group">
<!-- To be developed, linked to 'sample' model or separate? -->
<input type="file" name="file" id="field_file" />
</div>
<!-- The rest of the form -->
</form>
образец-update.component.ts
@Component({
selector: 'jhi-sample-update',
templateUrl: './sample-update.component.html'
})
export class SampleUpdateComponent implements OnInit {
// Variables, constructor, onInit(), ...
save() {
this.isSaving = true;
if (this.sample.id !== undefined) {
this.subscribeToSaveResponse(this.sampleService.update(this.sample));
} else {
this.subscribeToSaveResponse(this.sampleService.create(this.sample));
}
}
sample.service.ts
@Injectable({ providedIn: 'root' })
export class SampleService {
public resourceUrl = SERVER_API_URL + 'api/sample';
constructor(private http: HttpClient) {}
create(sample: ISample): Observable<EntityResponseType> {
const copy = this.convertDateFromClient(sample);
return this.http
.post<ISample>(this.resourceUrl, copy, { observe: 'response' })
.pipe(map((res: EntityResponseType) => this.convertDateFromServer(res)));
}
SampleResource.java
@RestController
@RequestMapping("/api")
public class SampleResource {
// Attributes and constructor
@PostMapping("/samples")
@Timed
public ResponseEntity<SampleDTO> createSample(@Valid @RequestBody SampleDTO sampleDTO) throws URISyntaxException {
log.debug("REST request to save Sample : {}", sampleDTO);
if (sampleDTO.getId() != null) {
throw new BadRequestAlertException("A new sample cannot already have an ID", ENTITY_NAME, "idexists");
}
SampleDTO result = sampleService.save(sampleDTO);
return ResponseEntity.created(new URI("/api/samples/" + result.getId()))
.headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
.body(result);
}