javascript - Ionic 4 上传截图到服务器

标签 javascript java angular mobile ionic4

我想截图并上传到服务器(我使用spring-boot),为此我使用原生库截图和它的 Angular 服务来获取图像URI,我转换了图像URI 到一个 blob,我使用 FORMDATA 发送它并发布 HTTPCLIENT 请求,问题出在后台,我没有找到名为文件的参数。拜托,任何人都可以帮助我吗?

注意:我使用 MULTIPARTFILE 作为 webservice 参数类型和 REQUESTPARAM 注释。

这里是java代码:

    @PostMapping(value = { "/uploadImg/{idColis}" })
public void uploadScreenShot(@RequestParam("file") MultipartFile file, @PathVariable String idColis) {
    if (file != null) {
        try {
            fileService.importerImage(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

使用的 Angular 代码:

  call(colis : any){
this.screenshot.URI(80).then(img => {
  this.screenShotsuccess = 'screened';
  this.colisService.upload(img,colis).subscribe(res=>{
    this.screenShotsuccess = 'screened and uploaded';
  });
}, err => {
  this.screenShotsuccess = err ;
} );}

upload(imgData : any,colis :any){

// Replace extension according to your media type
const imageName = colis.codeEnvoi+ '.jpg';
// call method that creates a blob from dataUri
const imageBlob = this.dataURItoBlob(imgData.URI);
const imageFile = new File([imageBlob], imageName, { type: 'image/jpeg' })

let  postData = new FormData();
postData.append('file', imageFile);

let data:Observable<any> = this.httpClient.post(this.wsListeUploadImage+colis.codeEnvoi,postData);
return data;}

  dataURItoBlob(dataURI) {
console.log(dataURI);
const byteString = window.atob(dataURI.split(',')[1]);
const arrayBuffer = new ArrayBuffer(byteString.length);
const int8Array = new Uint8Array(arrayBuffer);
for (let i = 0; i < byteString.length; i++) {
  int8Array[i] = byteString.charCodeAt(i);
 }
const blob = new Blob([int8Array], { type: 'image/jpeg' });    return blob;}

这是我得到的错误:

2019-12-29 08:21:07.276  WARN 5356 --- [nio-8080-exec-7] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present]

最佳答案

在您的 Angular 代码中,您正确地创建了 FormData,但您从未使用过它:

let data:Observable<any> = this.httpClient.post(this.wsListeUploadImage+colis.codeEnvoi,{'file':imageFile});

改成

let data:Observable<any> = this.httpClient.post(this.wsListeUploadImage+colis.codeEnvoi, postData);

关于javascript - Ionic 4 上传截图到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59503369/

相关文章:

javascript - D3圆包: html in text attribute

javascript - 如何使用 particles.js?

java - 优化数据库搜索查询

java - 如何更改 LayerDrawable 中一张图像的大小

angular - 多语言 @ngx-translate Ionic 3 Object(...) 不是 TranslateService.get 问题上的函数?

angular - 带有过滤器的搜索栏和带有 Ionic 2 的 JSON 数据

javascript - 悬停 img1 时显示文本 - 仅在悬停 img2 时隐藏

javascript - 将 Highcharts(和模块)与 lit-element 一起使用

java - 在 EBean 和表单中保存枚举列表

Angular 12 警告严重依赖 : the request of a dependency is an expression