首页 > 解决方案 > 如何使用离子角度将图像上传到谷歌驱动器

问题描述

如何使用离子角度(Android)将图像上传到谷歌驱动器。我需要将我的 ionic 应用程序连接到谷歌驱动器。从应用程序,图像被保存到谷歌驱动器。

标签: angularimageionic-frameworkuploaddrive

解决方案


您应该创建一个将图像放入 Google Drive 的后端服务。并将您的应用程序与此服务连接。

此案例的后端代码示例:

@RequestMapping(value="/savefile",method=RequestMethod.POST)  
public ModelAndView upload(@RequestParam CommonsMultipartFile file, HttpSession session) {  
    String path = session.getServletContext().getRealPath("/");  
    String filename = file.getOriginalFilename();  

    System.out.println(path+" "+filename);  
    try {  
        byte barr[] = file.getBytes();  

        // this code put the file in the path 
        BufferedOutputStream bout = new BufferedOutputStream(  
             new FileOutputStream(path + "/" + filename));  
        bout.write(barr);  
        bout.flush();  
        bout.close();  
    } catch(Exception e) {
        System.out.println(e);
    }  

    return new ModelAndView("upload-success", "filename", path + "/" + filename);  
}

您应该将将文件放入文件夹的代码更改为将文件存储在 Google Drive 中的另一个代码。

您的应用程序中的代码:

const httpOptions = {
  headers: new HttpHeaders({
    'Content-Type':  'application/json',
    'Access-Control-Allow-Origin': '*',
    'Access-Control-Allow-Methods': 'GET, PUT, POST, DELETE'
  })
};

uploadImage(url: string, param: any): Observable<any> {
  const body = JSON.stringify(param);
  console.log(body);
  return this.http
    .post(url, body, httpOptions);
}

uploadImage('Url to the back-service', image)
  .subscribe(
    result => {
      console.log('Image uploaded' + result);
    },
    error => console.log(error)
 );

推荐阅读