Cloudinary - 通过直接调用 API 上传,缺少文件
Cloudinary - Uploading with a direct call to the API, Missing file
嗨,我正在尝试从 React JS 使用 Cloudinary,所以我使用从浏览器直接调用 API。我有服务器,它返回我api_key等...
我正在使用readAsDataURL()将我的文件更改为base64
let file = e.target.files[0];
let self = this;
let typeString = this.tellType(file);
let reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function(){
self.sendMedia(typeString, this.result)
}
然后我从客户端nodejs服务器获取api_key,时间戳等,我需要发送图像,我需要将其发送到cloudinary。
let request = new XMLHttpRequest();
let params = {
api_key: result.upload_info.api_key,
timestamp: result.upload_info.timestamp,
signature: result.upload_info.signature,
public_id: result.upload_info.public_id,
file: file
};
console.log('params',params);
request.open('POST', `https://api.cloudinary.com/v1_1/${cloud_name}/image/upload`, true);
request.onreadystatechange = function() {
console.log('its back', request)
};
request.send(params);
然后我收到 403(错误请求)
responseText: "{"error":{"message":"Missing required parameter - file"}}"
我第一次认为我的文件格式错误,但 cloudniary 允许 base64。谢谢。
我遇到了同样的问题。碰巧您必须将参数作为 JSON 发送。
var reader = new FileReader();
reader.onload = function (e) {
//next line separates url from data
var form = new FormData();
form.append("file",e.target.result);
form.append("upload_preset", "preset_key");
currentRequest = $.ajax({
xhr: function () {
var xhr = new window.XMLHttpRequest();
xhr.upload.addEventListener("progress", function (evt) {
if (evt.lengthComputable) {
var progress = parseInt(evt.loaded / evt.total * 100, 10);
console.log(progress)
}
}, false);
return xhr;
},
async: true,
crossDomain: true,
url: 'https://api.cloudinary.com/v1_1/cloud_name/image/upload',
type: "POST",
headers: {
"cache-control": "no-cache"
},
processData: false,
contentType: false,
mimeType: "multipart/form-data",
data: form,
beforeSend: function () {
if (currentRequest != null) {
currentRequest.abort();
}
},
success: function (data) {
data=JSON.parse(data)
console.log(data['url'])
},
error: function (jqXHR, textStatus) {
console.log(jqXHR)
}
});
};
reader.readAsDataURL(this.files[0]);
您可以随时使用这个称为 cloudinary-direct 的软件包。
const Cloud = require('cloudniary-direct');
Cloud.cloudName = "your_cloud_name_from_cloudinary";
Cloud.api_key = "your_api_key_from_cloudinary";
Cloud.api_secret = "your_api_secret_from_cloudinary";
Cloud.upload_preset = "your_upload_preset_from_cloudinary";
如果你使用类似的东西 反应 那么这样做 .
import React from 'react';
import Cloud from 'cloudniary-direct';
import DropZone from 'react-dropzone';
class Sender extends React.Component{
constructor(){
super();
this.handler = this.handler.bind(this);
}
handler(file){
Cloud.cloudName = "your_cloud_name_from_cloudinary";
Cloud.api_key = "your_api_key_from_cloudinary";
Cloud.api_secret = "your_api_secret_from_cloudinary";
Cloud.upload_preset = "your_upload_preset_from_cloudinary";
Cloud.imageUploader(file[0], (resp)=> {
const imageURL = resp.secure_url;
// Save that url in the database
})
}
render(){
return (
<div>
<DropZone onDrop={this.handler} />
</div>
)
}
}
希望有帮助.
相关文章:
- 如何使用post方法从方法上传文件
- 使用Angular和Node上传文件
- 使用Angular js-量角器e2e测试上传文件
- 点击Angularjs中的按钮上传文件
- S3文件上传文件对象使用节点js
- AJAX上传文件不起作用
- jQuery使用$.post上传文件
- 使用node-js上传文件,并将表单信息存储在数据库(mysql)中
- ajax上传文件表单
- 为什么当我上传文件并点击更多上传另一个文件的第一个删除
- 使用单个按钮上传文件
- jQuery:无法通过输入点击上传文件
- AJAX下载后直接上传文件(无需存储)
- 使用Codeigner通过JQueryAJAX发送FormData和上传文件
- 如何在IE中使用JavaScript上传文件
- 在页面完成 PHP 处理之前捕获上传文件的时刻
- 如何使用火狐插件 SDK 从用户的系统上传文件
- 如何在 Codeigniter 中使用 ajax 上传文件
- 是否可以通过 AJAX *按顺序*上传文件
- 通过ajax上传文件不会在MVC中的请求中附加文件