我需要将textarea中的内容转换为file.txt,然后上传到服务器上(不用下载这个文件)。有沒有辦法用Angular或JS來做?
是可以的。在你的服务器端,接受内容为一个多部分的文件,像这样。
@RequestMapping(value = "/files", method = RequestMethod.POST)
public void uploadFile(
@RequestParam("file") MultipartFile file,
throws FileUploadFailedException, ContentTypeNotCorrectException {
}
在你的前端,代码如下。你需要从你的组件中调用这个服务。
upload(content: string, filename:string) {
const url = `${this.baseUrl}files`;
const headers = new HttpHeaders()
.set('Accept', 'application/json');
const formData: FormData = new FormData();
formData.append('file', content, filename);
const req = new HttpRequest('POST', url, formData, {
});