我在ember应用程序中使用dropzone.js上传文件。使用SpringBoot2.0作为后端服务器。尝试与spring boot集成时,文件上载不起作用。请查找以下代码以供参考,
Spring控制器
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<List<ImageResponseData>> uploadImage(
@RequestPart("file") MultipartFile multipartFile,
@RequestHeader(value = "pathVariable", required = false) String pathVariable,
@RequestPart(value = "metadatas",required = false) List<Map<String,String>> metadatas) throws ServiceException {
// process file and json to upload
}
js信息
didInsertElement() {
const url = this.get('url'),
target = this.$(`.${this.get('targetClass')}`).get(0),
self = this,
get = Ember.get;
this.set('register-as', this);
var dz = new Dropzone(target, {
url: url,
maxFiles: 1,
headers:{"pathVariable": self.pathVariable},
addRemoveLinks: true,
autoProcessQueue: false,
init() {
var file = self.get('images') || [];
this.options.addedfile.call(this, file);
let callback = null; // Optional callback when it's done
let crossOrigin = "Anonymous";
this.createThumbnailFromUrl(file, file.url, callback, crossOrigin);
this.emit("success". file);
this.emit("complete", file);
this.files.push(file);
}
});
dz.on("sending", function(file, xhr, formData) {
if (self.datas.length != 0) {
formData.append("metadatas", JSON.stringify(self.datas));
}
});
dz.on('success', (file, response) => {
console.log("file upload success:: ",file.id);
});
dz.on('error', (file, errorMessage, xhr) => {
console.log("file upload failed",errorMessage);
});
dz.on('removedfile', (file) => {
console.log('removedFile', file);
});
this.set('dropzone', dz);
},
这就是从浏览器提交数据的方式,
但是如果我尝试从postman上传数据到spring启动应用程序,它就可以工作了。
找到下面的图片作为参考上传数据通过 Postman
如果我尝试通过js上传它,它会抛出415。任何帮助都是值得赞赏的。
暂无答案!
目前还没有任何答案,快来回答吧!