angular - 我正在尝试在不使用 Cloudinary SDK 的情况下使用 Cloudinary post API 上传图像

标签 angular file-upload cloudinary

我是 cloudinary 和 Angular 的新手。我一直在寻找一种通过不使用 SDK 将图像上传到 cloudinary 的方法,因为 cloudinary 为我们提供了使用 post API 上传图像的选项,我尝试了以下但没有成功。

uploadImage(event) {
var that = this;
this.create_blob(event.srcElement.files[0], function (blob_string) {
  that.http.post('https://api.cloudinary.com/v1_1/image/upload/', { file: blob_string}).subscribe(res => {
    // url of upload file
  })
});

 create_blob(file, callback) {
var reader = new FileReader();
reader.onload = function () { callback(reader.result) };
reader.readAsDataURL(file);}

我在控制台中收到以下错误。

HttpErrorResponse {headers: HttpHeaders, status: 401, statusText: "Unauthorized", url: "https://api.cloudinary.com/v1_1/image/upload/", ok: false, …}

最佳答案

根据您提供的代码,我可以看到一些内容:

  • 您可以检查响应 header 并查找名为 X-Cld-Error 的 header 。这将提供有关请求失败原因的更多信息。在这种情况下,我希望它返回 cloud_name is disabled .当通过导致错误的 Cloudinary 交付 URL 请求资源时,也会出现此 header 。例如。 https://res.cloudinary.com/demo/image/upload/ww_350/sample.jpg会返回 X-Cld-Error header 包含 Invalid transformation parameter - ww .

  • 关于下一点,您使用的上传 API 端点不包含您的云名称。应该是https://api.cloudinary.com/v1_1/<cloud name>/image/upload

  • 客户端上传(即在没有身份验证签名的情况下执行的上传)需要在您的帐户中设置未签名的上传预设,并与 file 一起提供。作为两个强制参数。上传预设提供了一种定义上传参数的方法,这些参数通常允许在使用签名时直接在上传调用中设置。当不使用签名时,许多参数无法指定,如果需要,应在上传预设中配置。您可以在文档的这一部分找到更多相关信息,包括如何创建上传预设:https://cloudinary.com/documentation/upload_images#unsigned_upload

关于angular - 我正在尝试在不使用 Cloudinary SDK 的情况下使用 Cloudinary post API 上传图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58464638/

相关文章:

javascript - 替换 Cloudinary 上的图像

Angular 2 - 何时将服务放入组件的提供者与模块中?

components - Angular 2如何将变量从父组件传递到路由器导出

angular - 为什么 Chrome 在使用 angular service-worker 时会显示重复的 HTTP 请求?

javascript - 动态添加 span 标签到 <ng-content></ng-content> 内的链接

jquery-plugins - 如何使用 Cloudinary jQuery 插件直接从页面上传图像?

php - 如果具有该名称的图像已存在,则在文件名后附加一个数字

file-upload - Angular 2 : ng2-file-upload : cannot load it correctly using SystemJS

android - 如何增加android中改造库的上传时间

使用 maven 的 Java Cloudinary