【问题标题】:How to upload an image to the google drive using ionic angular如何使用离子角度将图像上传到谷歌驱动器
【发布时间】:2019-04-18 08:58:44
【问题描述】:

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

【问题讨论】:

    标签: angular image ionic-framework upload drive


    【解决方案1】:

    您应该创建一个将图像放入 Google 云端硬盘的后端服务。并将您的应用与此服务相关联。

    本案例的后端代码示例:

    @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)
     );
    

    【讨论】:

    • 实际上图像是在应用程序中生成的。我想将该图像保存到谷歌驱动器
    • 是的,你应该用图片向后台服务发起一个post请求,后台服务应该存储图片。我会在答案中举例说明。
    • 你应该看看这个页面:link@Beginner
    • 如果我们想在用户的驱动器中上传数据。所以如果用户想要他们可以自己删除它。这种情况不适用于此
    猜你喜欢
    • 2019-01-23
    • 1970-01-01
    • 2016-06-15
    • 1970-01-01
    • 1970-01-01
    • 2018-11-10
    • 2021-03-25
    • 1970-01-01
    • 2022-12-15
    相关资源
    最近更新 更多