function uploadFile(url,formData,f) {
var xhr = new XMLHttpRequest();
xhr.open('POST', url, true);
xhr.setRequestHeader("api-key","TEST");
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
// Update progress (can be used to show progress indicator)
xhr.upload.addEventListener("progress", function (e) {
//E(i, e.loaded * 100.0 / e.total || 100);
});
xhr.addEventListener('readystatechange', function (e) {
if (xhr.readyState == 4 && xhr.status == 200) {
var txt = xhr.responseText;
try{
txt= JSON.parse(txt)
}catch(e){}
f(txt);
} else
if (xhr.readyState == 4 && xhr.status != 200) {
f("error");
}
});
//send
xhr.send(formData);
}
//go
document.selectQuery(".inputfile").change = function(e){
var formData = new FormData();
data.append("file",this.files[0]);
uploadFile(url,formData,function(res){
console.log(res);
});
};
function postForm(u, _, E) {
$.ajax({
beforeSend: function (xhr) {
xhr.setRequestHeader('api-key', "TEST");
// Update progress (can be used to show progress indicator)
xhr.upload.addEventListener("progress", function (e) {
//f(i, e.loaded * 100.0 / e.total || 100);
});
},
type: "POST",
url: u,
global: false,
data: _,
processData: false,
contentType: false,
async: true,
success: function(u) {
try {
u = JSON.parse(u)
} catch (_) {}
E(u)
},
error: function(u, _, E) {
this.success(u.responseText)
}
})
}
//go
$(".inputfile").on("change",function(e){
var data = new FormData();
data.append("file",this.files[0]);
postForm(url,data,function(res){
console.log(res);
});
});
//go
document.selectQuery(".inputfile").change = function(e){
const form_data = new FormData();
form_data.append("file", this.files[0]);
const request_config = {
headers: {
"api-key": "TEST"
"Content-Type": "multipart/form-data"
},
data: form_data
};
axios.post(url, form_data, request_config).then(function(res){
});
};
import fs from "fs";
const form_data = new FormData();
form_data.append("file", fs.createReadStream(file.path));
const request_config = {
headers: {
"api-key": "TEST",
"Content-Type": "multipart/form-data"
},
data: form_data
};
//go
axios .post(url, form_data, request_config).then(function(res){
});
function post($file,$url,$data=[],$headers=array()){
$image = fopen($file, "rb");
$ch = curl_init();
curl_setopt($ch, CURLOPT_INFILE, $image);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers );
$result = curl_exec($ch);
$a = @json_decode($result);
return is_object($a)?$a:$result;
}
//how to use
$apikey="....";
$headers=["api-key: ".$apikey];
//
$data=[];
$file = "image.jpg";
//begin
$res = post($file,$url,$data,$headers);
if($res->code){
echo $res->url;
}else{
echo $res->error;
}
<script src="https://storage.donggiatri.com/sdk.min.js?callback=storage_callback"></script>
<script>
window.storage_callback = function(){
var server = {
apikey:"...",
host:"...",
zone:"vn",
user:{
_id:0,
name:"ABC"
}
};
//how to use
ServerStorage(server).then(function(io){
io.message(function(data){
});
//upload file
var data={
file : null,
progress: function(percent,loaded,total){
}
};
io.upload(data).then(function(res){
//your code
}).then(function(res){
//your code
});
});
};
</script>
Example