angular - 如何使用离子角度将图像上传到谷歌驱动器
问题描述
如何使用离子角度(Android)将图像上传到谷歌驱动器。我需要将我的 ionic 应用程序连接到谷歌驱动器。从应用程序,图像被保存到谷歌驱动器。
解决方案
您应该创建一个将图像放入 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)
);
推荐阅读
- c# - 在 WPF 中编辑名称后,ComboBox Selected Item 正在工作
- r - 如何读取包含两行以上数据的csv文件
- java - 使用 RESTAssured 从 HTML 获取 XML 结果
- c++ - 重新增长数组时出错:写入时缓冲区溢出
- scala - 需要帮助创建一个虚拟 WSResponse 对象(Scala + Play)
- xslt - 使用纯 XSL 创建 MD5 加密函数
- python - Python 3.x 的最低版本 RH
- python - 是否有可能让 python PyDoc 模块返回一个包含内容的字符串而不是仅仅打印它?
- php - 如何在 Codeigniter 中连接 mssql?
- errbit - nil 的未定义方法“each”:errbit 上的 NilClass 错误