Skip to content

Commit

Permalink
feat: example base64 to blob and doc (#434)
Browse files Browse the repository at this point in the history
Android Browser not support new File constructor
  • Loading branch information
binghaiwang authored and PeterRao committed Apr 16, 2018
1 parent 23ef84f commit 37f65f0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ Add an object to the bucket.
parameters:

- name {String} object name store on OSS
- file {String|Buffer|ReadStream} object local path, content buffer or ReadStream content instance
- file {String|Buffer|ReadStream|File(only support Browser)|Blob(only support Browser)} object local path, content buffer or ReadStream content instance use in Node, Blob and html5 File
- [options] {Object} optional parameters
- [timeout] {Number} the operation timeout
- [mime] {String} custom mime, will send with `Content-Type` entity header
Expand Down Expand Up @@ -1793,7 +1793,7 @@ this function contains initMultipartUpload, uploadPart, completeMultipartUpload.
parameters:

- name {String} object name
- file {String|File} file path or HTML5 Web File
- file {String|File(only support Browser)|Blob(only support Browser)} file path or HTML5 Web File or web Blob
- [options] {Object} optional args
- [parallel] {Number} the number of parts to be uploaded in parallel
- [partSize] {Number} the suggested size for each part
Expand Down
27 changes: 14 additions & 13 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';


// require("babel-polyfill")

Expand Down Expand Up @@ -29,15 +29,15 @@ const applyTokenDo = function (func, refreshSts) {
if (refresh) {
const url = appServer;
return $.ajax({
url
url,
}).then((result) => {
const creds = result;
const client = new OSS.Wrapper({
region,
accessKeyId: creds.AccessKeyId,
accessKeySecret: creds.AccessKeySecret,
stsToken: creds.SecurityToken,
bucket
bucket,
});

console.log(OSS.version);
Expand Down Expand Up @@ -73,8 +73,8 @@ const uploadFile = function (client) {
partSize: 100 * 1024,
meta: {
year: 2017,
people: 'test'
}
people: 'test',
},
};
if (currentCheckpoint) {
options.checkpoint = currentCheckpoint;
Expand Down Expand Up @@ -116,32 +116,33 @@ function dataURLtoFile(dataurl, filename) {
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new File([u8arr], filename, { type: mime });
return new Blob([u8arr], { type: mime });// if env support File, also can use this: return new File([u8arr], filename, { type: mime });
}


const uploadBase64Img = function (client) {
const base64Content = document.getElementById('base64-file-content').value.trim();
const key = document.getElementById('base64-object-key-file').value.trim() || 'object';
if (base64Content.startsWith('data:image')) {
if (base64Content.indexOf('data:image') === 0) {
const imgfile = dataURLtoFile(base64Content, 'img.png');
client.multipartUpload(key, imgfile, {
progress: base64progress
progress: base64progress,
}).then((res) => {
console.log('upload success: %j', res);
}).catch((err) => {
console.error(err);
});
} else {
alert('Please fill in the correct Base64 img');
}
alert('Please fill in the correct Base64 img');
};

const listFiles = function (client) {
const table = document.getElementById('list-files-table');
console.log('list files');

return client.list({
'max-keys': 100
'max-keys': 100,
}).then((result) => {
const objects = result.objects.sort((a, b) => {
const ta = new Date(a.lastModified);
Expand Down Expand Up @@ -180,7 +181,7 @@ const uploadBlob = function (client) {
console.log(`content => ${key}`);

return client.put(key, new Blob([content], { type: 'text/plain' })).then(res => listFiles(client));
}
};


const downloadFile = function (client) {
Expand All @@ -190,8 +191,8 @@ const downloadFile = function (client) {

const result = client.signatureUrl(object, {
response: {
'content-disposition': `attachment; filename="${filename}"`
}
'content-disposition': `attachment; filename="${filename}"`,
},
});
window.location = result;

Expand Down

0 comments on commit 37f65f0

Please sign in to comment.